iOS 动画, A的动画,ApushB,BpopA动画不再执行

先 摘抄一个网上
一:
背景:我做一个二维码扫描的动画,就是一根“线”,不断上下移动

上代码:

//扫描动画
- (void)scanAnimation
{
    _sliceView.frame = CGRectMake((SCREEN_WIDTH-kOverFrameWidth)/2, kSliceViewY_top, kOverFrameWidth-3, kSliceHeight);
    [UIView animateWithDuration:1.5f delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveLinear|UIViewAnimationOptionAllowUserInteraction animations:^{
         _sliceView.frame = CGRectMake(_sliceView.frame.origin.x,kSliceViewY_bottom,_sliceView.frame.size.width,_sliceView.frame.size.height);
    } completion:^(BOOL finished) {
 
    }];
}

bug:

1.进入后台回来, “线”不动了。
解决

- (void)viewDidLoad{ 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scanAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
}
 - (void)viewDidAppear:(BOOL)animated{
    [self scanAnimation];
}

所以我想到的就是用通知。

2.从动画界面进入另一个界面,返回后动画停止
解决:

- (void)viewDidAppear:(BOOL)animated{
    [self scanAnimation];
}

\color{#FF0000} {注意:重新调用动画的方法一定要放在viewDidAppear方法中,放在viewWillAppear中不会起作用的(血的教训啊!!)}

二 再添加一个我曾经遇到的一个问题
背景:
一个旋转动画.再跳转到其他面返回后 执行动画, 无效果
先上代码

//MARK: 开始动画
-(void)begenRotation{
    CAAnimation *animation = [self.layer animationForKey:@"rotationAnimation"];
    
    if (animation) {
        CFTimeInterval pauseTime = self.layer.timeOffset;
        CFTimeInterval begin = CACurrentMediaTime() - pauseTime;
        [self.layer setTimeOffset:0];
        [self.layer setBeginTime:begin];
        self.layer.speed = 1;
    }else{
        CABasicAnimation *rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
        rotationAnimation.duration = 0.5;
        rotationAnimation.repeatCount = HUGE_VALF;
        [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
}

//MARK: 停止动画

-(void)stopRotation{
    CFTimeInterval pauseTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //2.设置动画的时间偏移量,指定时间偏移量的目的是让动画定格在该时间点的位置
    self.layer.timeOffset = pauseTime;
    //3.将动画的运行速度设置为0, 默认的运行速度是1.0
    self.layer.speed = 0;
}

上面的代码 再当前页面 开始和暂停都是可以的 . 再push到其他页面再pop回来之后,就不执行了 .

CAAnimation *animation = [self.layer animationForKey:@"rotationAnimation"];
//这个animation  不存在了. 

经查, 是这个动画默认 完成后 移除了 . 而我上述 代码只是暂停了 并没有 停止或者完成动画.
是默认YES的

rotationAnimation.removedOnCompletion = NO; 
-(void)begenRotation{
    CAAnimation *animation = [self.layer animationForKey:@"rotationAnimation"];
    
    if (animation) {
        CFTimeInterval pauseTime = self.layer.timeOffset;
        CFTimeInterval begin = CACurrentMediaTime() - pauseTime;
        [self.layer setTimeOffset:0];
        [self.layer setBeginTime:begin];
        self.layer.speed = 1;
    }else{
        CABasicAnimation *rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
        rotationAnimation.duration = 0.5;
        rotationAnimation.repeatCount = HUGE_VALF;
        rotationAnimation.removedOnCompletion = NO;
        [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
}

你可能感兴趣的:(iOS 动画, A的动画,ApushB,BpopA动画不再执行)