iOS:CAKeyframeAnimation实现UIImageView精准时间的动画结束事件

本文介绍如何利用CAAnimation在iOS应用中实现动画结束的精确控制,通过使用animationDidStop方法,开发者可以在动画结束时执行特定的操作,避免了传统方法的延迟误差或不优雅的timer解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


         由于项目 界面效果 的需要,不能用gif来展示开机动画(动画质量,颜色差太多),只能一帧一帧图片显示。所以自然想到UIImageView的animationImages来做动画。不过系统没提供具体的结束事件,只能用 延时(会有0.0x秒的误差) 或 不优雅的timer 来做。
        为了动画结束时的精准控制(心中一万只草泥马踏过策划),请教组里bigshot,学到了CAAnimation提供的animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;方法,可以精确知道动画结束事件。
       OK,Talk is cheap,CODE SHOW~

- (void)viewDidLoad {

  //创建UIImageView
  UIImageView *openningAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,kUIScreen_Width,kUIScreen_Height)];
    openningAnimation.image = [UIImage imageNamed:@"image_last.png"];
   [self.view addSubview:openningAnimation];

  //创建CAKeyframeAnimation
  CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
   animation.duration = animationTime;
   animation.delegate = self; 

  //存放图片的数组
   NSMutableArray *array = [NSMutableArray array];
   for(NSUInteger i = 0;i < count ;i++) {
       
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:"image_%d.png",i]];
        CGImageRef cgimg = image.CGImage;
        [array addObject:(__bridge UIImage *)cgimg];
   }

   animation.values = array;

   [openningAnimation.layer addAnimation:animation forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

   //clearMemory
   //结束事件
}

记得在动画结束时clearMemory ,因为那些高清无码大图超占内存。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值