zoukankan      html  css  js  c++  java
  • 在ios中运用core animation暂停和继续动画

    本文转载至 http://blog.csdn.net/wildfireli/article/details/23191861

     

    暂停和继续动画的核心代码如下:  

    [cpp] view plaincopy
     
    1. <pre name="code" class="cpp">//暂停layer上面的动画  
    2. - (void)pauseLayer:(CALayer*)layer  
    3. {  
    4.     CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];  
    5.     layer.speed = 0.0;  
    6.     layer.timeOffset = pausedTime;  
    7. }  
    8.   
    9. //继续layer上面的动画  
    10. - (void)resumeLayer:(CALayer*)layer  
    11. {  
    12.     CFTimeInterval pausedTime = [layer timeOffset];  
    13.     layer.speed = 1.0;  
    14.     layer.timeOffset = 0.0;  
    15.     layer.beginTime = 0.0;  
    16.     CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;  
    17.     layer.beginTime = timeSincePause;  
    18. }</pre><br>  
    19. <pre></pre>  
    20. <p>附上完整代码</p>  
    21. <p></p>  
    22. <p class="p1">AnimationPauseViewController.h</p>  
    23. <pre name="code" class="cpp">#import <UIKit/UIKit.h>  
    24.   
    25. @interface AnimationPauseViewController : UIViewController {  
    26.       
    27.     UIImageView *soccer;  
    28.     BOOL isPause;  
    29.     UIButton *controlButton;  
    30. }  
    31. @property (nonatomic, retain) IBOutlet UIImageView *soccer;  
    32. - (IBAction)clickControlButton:(id)sender;  
    33. @property (nonatomic, retain) IBOutlet UIButton *controlButton;  
    34.   
    35. @end</pre><br>  
    36. <p></p>  
    37. <p class="p1">AnimationPauseViewController.m</p>  
    38. <p></p><pre name="code" class="cpp">#import "AnimationPauseViewController.h"  
    39. #import <QuartzCore/QuartzCore.h>  
    40.   
    41. @implementation AnimationPauseViewController  
    42. @synthesize controlButton;  
    43. @synthesize soccer;  
    44.   
    45. - (void)dealloc  
    46. {  
    47.     [soccer release];  
    48.     [controlButton release];  
    49.     [super dealloc];  
    50. }  
    51.   
    52. - (void)didReceiveMemoryWarning  
    53. {  
    54.     // Releases the view if it doesn't have a superview.  
    55.     [super didReceiveMemoryWarning];  
    56.       
    57.     // Release any cached data, images, etc that aren't in use.  
    58. }  
    59.   
    60. - (void)addAnimations  
    61. {  
    62.     //让足球来回移动  
    63.     CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];  
    64.     translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(24, 240)];  
    65.     translation.toValue = [NSValue valueWithCGPoint:CGPointMake(320- 24, 240)];  
    66.     translation.duration = 2;  
    67.     translation.repeatCount = HUGE_VALF;  
    68.     translation.autoreverses = YES;  
    69.       
    70.     //让足球来回转动  
    71.     CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  
    72.     //kCAMediaTimingFunctionLinear 表示时间方法为线性,使得足球匀速转动  
    73.     rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];  
    74.     rotation.toValue = [NSNumber numberWithFloat:4 * M_PI];  
    75.     rotation.duration = 2;  
    76.     rotation.repeatCount = HUGE_VALF;  
    77.     rotation.autoreverses = YES;  
    78.       
    79.     [soccer.layer addAnimation:rotation forKey:@"rotation"];  
    80.     [soccer.layer addAnimation:translation forKey:@"translation"];  
    81. }  
    82.   
    83. #pragma mark - View lifecycle  
    84. - (void)viewDidLoad  
    85. {  
    86.     [super viewDidLoad];  
    87.     [self addAnimations];  
    88. }  
    89.   
    90.   
    91. - (void)viewDidUnload  
    92. {  
    93.     [self setSoccer:nil];  
    94.     [self setControlButton:nil];  
    95.     [super viewDidUnload];  
    96. }  
    97.   
    98. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    99. {  
    100.     // Return YES for supported orientations  
    101.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
    102. }  
    103.   
    104. //暂停layer上面的动画  
    105. - (void)pauseLayer:(CALayer*)layer  
    106. {  
    107.     CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];  
    108.     layer.speed = 0.0;  
    109.     layer.timeOffset = pausedTime;  
    110. }  
    111.   
    112. //继续layer上面的动画  
    113. - (void)resumeLayer:(CALayer*)layer  
    114. {  
    115.     CFTimeInterval pausedTime = [layer timeOffset];  
    116.     layer.speed = 1.0;  
    117.     layer.timeOffset = 0.0;  
    118.     layer.beginTime = 0.0;  
    119.     CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;  
    120.     layer.beginTime = timeSincePause;  
    121. }  
    122.   
    123. - (void)pauseSoccer  
    124. {  
    125.     isPause = YES;  
    126.     [controlButton setTitle:@"继续" forState:UIControlStateNormal];  
    127.     [self pauseLayer:soccer.layer];  
    128. }  
    129.   
    130. - (void)resumeSoccer  
    131. {  
    132.     isPause = NO;  
    133.     [controlButton setTitle:@"暂停" forState:UIControlStateNormal];  
    134.     [self resumeLayer:soccer.layer];  
    135. }  
    136.   
    137. - (IBAction)clickControlButton:(id)sender {  
    138.     if (isPause) {  
    139.         [self resumeSoccer];  
    140.     }else{  
    141.         [self pauseSoccer];  
    142.     }  
    143. }  
    144. @end</pre><br>  
    145. <br>  
    146. <p></p>  
  • 相关阅读:
    python分析文本文件/json
    python中文件操作
    python异常处理
    socket网络模块
    层模型--固定定位
    层模型--相对定位
    层模型--绝对定位
    什么是层模型?
    浮动模型
    流动模型/a标签换行问题
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4326186.html
Copyright © 2011-2022 走看看