CABasicAnimation 暂停和继续

  1. #import <UIKit/UIKit.h>  
  2. @interface AnimationPauseViewController : UIViewController {  
  3.       
  4.     UIImageView *soccer;  
  5.     BOOL isPause;  
  6.     UIButton *controlButton;  
  7. }  
  8. @property (nonatomic, retain) IBOutlet UIImageView *soccer;  
  9. - (IBAction)clickControlButton:(id)sender;  
  10. @property (nonatomic, retain) IBOutlet UIButton *controlButton;  
  11.   
  12. @end
  13. #import "AnimationPauseViewController.h"  
  14. #import <QuartzCore/QuartzCore.h>  
  15.   
  16. @implementation AnimationPauseViewController  
  17. @synthesize controlButton;  
  18. @synthesize soccer;  
  19.   
  20. - (void)addAnimations  
  21. {  
  22.     //让足球来回移动  
  23.     CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];  
  24.     translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(24, 240)];  
  25.     translation.toValue = [NSValue valueWithCGPoint:CGPointMake(320- 24, 240)];  
  26.     translation.duration = 2;  
  27.     translation.repeatCount = HUGE_VALF;  
  28.     translation.autoreverses = YES;  
  29.       
  30.     //让足球来回转动  
  31.     CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
  32.     //kCAMediaTimingFunctionLinear 表示时间方法为线性,使得足球匀速转动  
  33.     rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];  
  34.     rotation.toValue = [NSNumber numberWithFloat:4 * M_PI];  
  35.     rotation.duration = 2;  
  36.     rotation.repeatCount = HUGE_VALF;  
  37.     rotation.autoreverses = YES;  
  38.       
  39.     [soccer.layer addAnimation:rotation forKey:@"rotation"];  
  40.     [soccer.layer addAnimation:translation forKey:@"translation"];  
  41. }  
  42.   
  43. #pragma mark - View lifecycle  
  44. - (void)viewDidLoad  
  45. {  
  46.     [super viewDidLoad];  
  47.     [self addAnimations];  
  48. }  
  49.   
  50. //暂停layer上面的动画  
  51. - (void)pauseLayer:(CALayer*)layer  
  52. {  
  53.     CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];  
  54.     layer.speed = 0.0;  
  55.     layer.timeOffset = pausedTime;  
  56. }  
  57.   
  58. //继续layer上面的动画  
  59. - (void)resumeLayer:(CALayer*)layer  
  60. {  
  61.     CFTimeInterval pausedTime = [layer timeOffset];  
  62.     layer.speed = 1.0;  
  63.     layer.timeOffset = 0.0;  
  64.     layer.beginTime = 0.0;  
  65.     CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;  
  66.     layer.beginTime = timeSincePause;  
  67. }  
  68.   
  69. - (void)pauseSoccer  
  70. {  
  71.     isPause = YES;  
  72.     [controlButton setTitle:@"继续" forState:UIControlStateNormal];  
  73.     [self pauseLayer:soccer.layer];  
  74. }  
  75.   
  76. - (void)resumeSoccer  
  77. {  
  78.     isPause = NO;  
  79.     [controlButton setTitle:@"暂停" forState:UIControlStateNormal];  
  80.     [self resumeLayer:soccer.layer];  
  81. }  
  82.   
  83. - (IBAction)clickControlButton:(id)sender {  
  84.     if (isPause) {  
  85.         [self resumeSoccer];  
  86.     }else{  
  87.         [self pauseSoccer];  
  88.     }  
  89. }  
  90. @end

posted on 2015-07-21 11:03  代码改变宇宙  阅读(472)  评论(0)    收藏  举报