如何暂停UIView动画

时间:2012-07-10 09:59:09

标签: ios animation uiview

在iOS5.0中使用块,比如我们有一个UIViewAnimationOptionAutoreverse动画,并且需要在反向动画开始之前加入延迟..我们怎么能这样做?

感谢您对此的帮助

5 个答案:

答案 0 :(得分:2)

The best way to pause and resume animation :

add QuartzCore framework 


then use the following apple's code 

-(void) pause
{

CFTimeInterval pausedTime = [m_pCustomImageBtnObj.layer convertTime:CACurrentMediaTime() fromLayer:nil];

    obj.layer.speed = 0.0;

    obj.layer.timeOffset = pausedTime;
}


-(void) resume
{

CFTimeInterval pausedTime = [obj.layer timeOffset];

    obj.layer.speed = 1.0;

    obj.layer.timeOffset = 0.0;

    obj.layer.beginTime = 0.0;

    CFTimeInterval timeSincePause = [obj.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;

    obj.layer.beginTime = timeSincePause;


}

答案 1 :(得分:0)

您可以尝试这样的动画完成块:

[UIView animateWithDuration:0.6 animations:^(void)
 {
     self.label.bounds = CGRectMake(10, 10, 200, 200); 

 }completion:^(BOOL finished) {
     [UIView animateWithDuration:0.6 animations:^(void)
      {
          self.label.bounds = CGRectMake(10, 10, 100, 100); 

      }completion:^(BOOL finished) {

      }];

 }];

答案 2 :(得分:0)

你应该使用关键帧动画。这是一段用反转进行旋转的代码:

self.animations = [NSDictionary dictionaryWithObjects:
                   [NSArray arrayWithObjects:
                    [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"],
                    [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]                         

                    , nil ]
                                              forKeys:
                   [NSArray arrayWithObjects: THERE_ANIM_KEY, BACK_AGAIN_ANIM_KEY , nil ]];
CAKeyframeAnimation *kfAnim = [animations objectForKey: THERE_ANIM_KEY];
kfAnim.duration = 2;
kfAnim.repeatCount = 1;
kfAnim.values = [NSArray arrayWithObjects: 
                 [NSNumber numberWithFloat: 0.0 * M_PI],
                 [NSNumber numberWithFloat: 1.0 * M_PI],
                 [NSNumber numberWithFloat: 2.0 * M_PI],
                 [NSNumber numberWithFloat: 2.0 * M_PI], //here is delay
                 [NSNumber numberWithFloat: 1.0 * M_PI],
                 [NSNumber numberWithFloat: 0.0 * M_PI],
                 nil];
 kfAnim.keyTimes = [NSArray arrayWithObjects: 
 [NSNumber numberWithFloat: 0.0 ],
 [NSNumber numberWithFloat: 0.3 ],     
 [NSNumber numberWithFloat: 0.5 ],  //time of delaying in proportion of general animation duration   
 [NSNumber numberWithFloat: 0.5 ],
 [NSNumber numberWithFloat: 0.8 ],
 [NSNumber numberWithFloat: 1.0 ], nil];

此外,你可以使用记住初始状态和动画回来

  
      
  • (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
  •   

使用performSelector的动画委托方法:withObject:afterDelay:。这更加简单,易于设置延迟。

答案 3 :(得分:0)

不确定这是否与您尝试做的完全相同,但这是我用于将视图移动到屏幕上,暂停1秒钟,然后将视图从屏幕上移开的方法。

[UIView animateWithDuration:0.5 animations:^{
    self.view.frame = viewFrameOnScreen;
    self.view.alpha = 1.0;
} completion:^(BOOL finished){
    [UIView animateWithDuration:0.5 delay:1 options:0 animations:^{
        self.view.frame = viewFrameOffScreen;
        self.view.alpha = 0.0f;

    } completion:^(BOOL finished){}];
}];

答案 4 :(得分:0)

RIAZ回答Swift 3:

func pauseAnimation() {
    let pausedTime = obj.layer.convertTime(CACurrentMediaTime(), from: nil)
    obj.layer.speed = 0.0
    obj.layer.timeOffset = pausedTime
}

func resumeAnimation() {
    let pausedTime = obj.layer.timeOffset
    obj.layer.speed = 1.0
    obj.layer.timeOffset = 0.0
    obj.layer.beginTime = 0.0
    let timeSincePause = obj.layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
    obj.layer.beginTime = timeSincePause
}