ios 移除动画

在移除动画之前, 首先得在 layer 上添加两个动画
示例代码:

CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation";
animation.duration = 2.0;
animation.byValue = @(M_PI * 2);
animation.delegate = self;

animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.layer addAnimation:animation forKey:@"rotateAnimation"];

CABasicAnimation *animation2 = [CABasicAnimation animation];
animation2.keyPath = @"transform.scale";
animation2.duration = 2.0;
animation2.toValue = @2.0;
animation2.delegate = self;

animation2.removedOnCompletion = NO;
animation2.fillMode = kCAFillModeForwards;
[self.layer addAnimation:animation2 forKey:@"scale"];

添加两个按钮来控制动画的 开始 和 移除 操作, 代码(略)

点击开始, 执行上面的代码

点击结束:
1.移除制定动画

[self.layer removeAnimationForKey:@"rotateAnimation"];

2.移除所有动画

[self.layer removeAllAnimations];

查看动画结束时的打印:

#pragma mark Animation delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSString *strAnimation = nil;
    if ([self.layer animationForKey:@"rotateAnimation"] == anim) {
        strAnimation = @"rotateAnimation";
    }
    if ([self.layer animationForKey:@"scale"] == anim){
        strAnimation = @"scaleAnimation";
    }
    NSLog(@"%@ stop finish: %@",strAnimation, flag? @"yes": @"no");
}

特别注意:
1.以上方法取值时 使用的是 animationForKey 而不是 valueForKey
2.一定要把 animation 的 removedOnCompletion 设置成 NO, 否则取不到值

执行以上代码后, 发现手动结束动画时, 图像会还原到原来的位置.
解决方法:
在移除动画操作后添加以下代码

// 动画移除后 保持在当前位置
self.layer.transform = self.layer.presentationLayer.transform;

执行之后效果如下:

效果图.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,155评论 1 23
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,566评论 6 30
  • 一、简介 Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽...
    莦婼姑娘阅读 978评论 0 4
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 3,056评论 0 21
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13