iPhone开发创建连续动画案例

本文介绍在iOS开发中如何通过扩展UIView类来创建连续动画,避免动画重叠,提供了一种简单易用的实现方式。通过引入新的方法commitModalAnimations,开发者可以在不修改现有代码的情况下实现动画的有序播放。

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

iPhone开发创建连续动画案例是本文要介绍的内容,主要详细介绍了在iphone开发中连续动画实现  。来看详细内容  。在iphone开发中,我们有些时候需要创建连续的动画效果,使用户体验更好  。

  连续动画就是一段动画运行完毕后调用另一段动画,一定要保证两段动画没有重叠,本文不打算使用CAKeyframeAnimation建立连续的动画块,虽然使用CAKeyframeAnimation更简单(在其他的博文中实现此方法

  大概有两种方法可以选择:

  1.增加延迟以便在第一段动画结束之后在启动第二段动画([performSelector:withObject:afterDelay:])

  2.指定动画委托回调(animationDidStop:finished:context:)

  从编程的角度来说就更容易,下面我们将扩展UIView的方法,通过类别引入一个新的方法----commitModalAnimations.调用此方法而不是调用commitAnimations方法,它会建立一个新的runloop,该循环只有在动画结束的时候才会停止  。

  这样确保了commitModalAnimations方法只有在动画结束才将控制权返还给调用方法,利用此扩展方法可以将动画块按顺序放进代码中,不必做任何其他的修改就能避免动画的重叠现象  。

  代码如下:

 
  1. @interface UIView (ModalAnimationHelper)  
  2. + (void) commitModalAnimations;  
  3. @end  
  4. @interface UIViewDelegate : NSObject  
  5. {  
  6. CFRunLoopRef currentLoop;  
  7. }  
  8. @end  
  9. @implementation UIViewDelegate  
  10. -(id) initWithRunLoop: (CFRunLoopRef)runLoop   
  11. {  
  12. if (self = [super init]) currentLoop = runLoop;  
  13. return self;  
  14. }  
  15. (void) animationFinished: (id) sender  
  16. {  
  17. CFRunLoopStop(currentLoop);  
  18. }  
  19. @end  
  20. @implementation UIView (ModalAnimationHelper)  
  21. + (void) commitModalAnimations  
  22. {  
  23. CFRunLoopRef currentLoop = CFRunLoopGetCurrent();  
  24. UIViewDelegate *uivdelegate = [[UIViewDelegate alloc] initWithRunLoop:currentLoop];  
  25. [UIView setAnimationDelegate:uivdelegate];  
  26. [UIView setAnimationDidStopSelector:@selector(animationFinished:)];  
  27. [UIView commitAnimations];  
  28. CFRunLoopRun();  
  29. [uivdelegate release];  
  30. }  
  31. @end 

  使用:

 
  1. [self.view viewWithTag:101].alpha = 1.0f;  
  2. // Bounce to 115% of the normal size  
  3. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  4. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  5. [UIView setAnimationDuration:0.4f];  
  6. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.15f, 1.15f);  
  7. [UIView commitModalAnimations];  
  8. // Return back to 100%  
  9. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  10. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  11. [UIView setAnimationDuration:0.3f];  
  12. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.0f, 1.0f);  
  13. [UIView commitModalAnimations];  
  14. // Pause for a second and appreciate the presentation  
  15. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];  
  16. // Slowly zoom back down and hide the view  
  17. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  18. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  19. [UIView setAnimationDuration:1.0f];  
  20. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(0.01f, 0.01f);  
  21. [UIView commitModalAnimations];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值