2014-04-10 4 views
1

다음과 같은 애니메이션 블록을 사용하여 특정 윤곽 레이블 효과를 적용 할 수 있습니다.
두 개의 레이블이 있는데 두 개 모두 스크롤합니다. 스크롤 애니메이션이 완료되면 새 프레임을 설정하고 다시 스크롤합니다. 애니메이션이 멈추지 않습니다.UIView 애니메이션 대신 CABasicAnimation

- (void)scrollAwayWithInterval:(NSTimeInterval)interval delay:(NSTimeInterval)delay { 
    [UIView animateWithDuration:interval 
          delay:delay 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 
         self.label.frame = _labelDestRect; 
         self.subLabel.frame = _subLabelDestRect; 
        } 
        completion:^(BOOL finished) { 
         if (finished) { 
          if (self.subLabel.frame.origin.x > 0) { 
           [self prepareFramesForStep2]; 
          } else { 
           [self prepareFramesForStep1]; 
          } 

          [self scrollAwayWithInterval:interval delay:delay]; 
         } 
        }]; 
} 

나는/일시 정지 애니메이션을 다시 시작하는 방법이 필요합니다, 그래서 나는 CABasicAnimation와 같은 애니메이션을 수행합니다.

제 질문은 CABasicAnimation으로 다음 애니메이션을 수행하는 동일한 방법은 무엇입니까?

+0

당신이 묻는 또는 코어 애니메이션 코드로 코드를 변환하는 우리에게 요구하고있다? ;) –

+0

@ DavidRönnqvist 두 번째 옵션. 예를 들어, 가능하면 애니메이션의 완성 블록을 가질 수 있을지 모르겠다. – Mario

답변

2

실제로 UIView 애니메이션을 일시 중지 할 수 있습니다. 그것은 커버 아래에 있기 때문에, IOS는 당신이 움직이고있는 뷰 (들)의 레이어에 CAAnimations를 생성하기 때문에 작동합니다. 나는 정확하게 이것을 증명하는 KeyframeViewAnimations이라는 github에 대한 샘플 프로젝트를 가지고 있습니다.

두 개의 서로 다른 뷰에 동시에 애니메이션을 적용하는 경우 두 뷰가 포함 된 수퍼 뷰의 레이어에서 속도 속성을 조작하거나 두 뷰의 레이어에서 애니메이션을 별도로 일시 중지해야 할 수 있습니다. (나는 이전에 동시에 여러 뷰에서 UIView 애니메이션을 일시 중지하지 않았으므로 superview의 레이어에서 speed 속성을 설정하면 작동 여부가 확실하지 않습니다.)

편집 : 방금 해봤습니다. , 그리고 superview의 레이어에 속도를 설정하면 두 애니메이션이 일시 중지됩니다. 나는 일시 정지/다시 시작 버튼을 넣어 일시 정지/다시 시작 코드는 다음과 같습니다 : 선형이 무한히 반복 CABasicAnimation이있는 경우

- (IBAction)handlePauseButton:(UIButton *)sender 
{ 
    animationIsPaused = !animationIsPaused; 
    if (animationIsPaused) 
    { 
    [pauseButton setTitle: @"Resume" forState: UIControlStateNormal]; 
    CALayer *theLayer = animationContainerView.layer; 

    //Freeze the animation. 
    theLayer.speed = 0; 

    //Calculate how far we into the animation based on current media time minus the media time when 
    //The animation started. 

    //Shift the layer's timing so it appears at the current time. 
    theLayer.timeOffset = CACurrentMediaTime()-theLayer.beginTime; 
    theLayer.beginTime = 0; 
    } 
    else 
    { 
    [pauseButton setTitle: @"Pause" forState: UIControlStateNormal]; 
    CALayer *theLayer = animationContainerView.layer; 

    CFTimeInterval pausedTime = [theLayer timeOffset]; 

    //Now reset the time offset and beginTime to zero. 
    theLayer.timeOffset = 0.0; 
    theLayer.beginTime = 0.0; 

    //Figure out how much time has elapsed since the animation was paused. 
    CFTimeInterval timeSincePause = CACurrentMediaTime() - pausedTime; 

    //Set the layer's beginTime to that time-since-pause 
    theLayer.beginTime = timeSincePause; 

    //Start the animation running again 
    theLayer.speed = 1.0; 
    } 
} 
1

이 블록을 계속 사용할 수 있습니다.

추가 할 필요

은 버튼을 한 번 눌러서에서 NO로 설정할 수 BOOL 변수 (또는 그러나 당신이 원하는)와 단지로 설정 :

- (void)scrollAwayWithInterval:(NSTimeInterval)interval delay:(NSTimeInterval)delay { 
    [UIView animateWithDuration:interval 
          delay:delay 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 
         self.label.frame = _labelDestRect; 
         self.subLabel.frame = _subLabelDestRect; 
        } 
        completion:^(BOOL finished) { 
         if (finished) { 
          if (shouldContinueAnimation) { 
           if (self.subLabel.frame.origin.x > 0) { 
            [self prepareFramesForStep2]; 
           } else { 
            [self prepareFramesForStep1]; 
           } 

           [self scrollAwayWithInterval:interval delay:delay]; 
          } 
         } 
        }]; 
} 

shouldContinueAnimation은 BOOL입니다 .

희망이 있습니다.

+0

애니메이션을 일시 정지 할 수 있기를 원한다. 즉, 정지 한 위치에서 정지해야한다는 의미이다. 솔루션은 완료 될 때까지 기다립니다. – Mario