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

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

  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. //继续layer上面的动画
  9. - (void)resumeLayer:(CALayer*)layer
  10. {
  11. CFTimeInterval pausedTime = [layer timeOffset];
  12. layer.speed = 1.0;
  13. layer.timeOffset = 0.0;
  14. layer.beginTime = 0.0;
  15. CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
  16. layer.beginTime = timeSincePause;
  17. }</pre><br>
  18. <pre></pre>
  19. <p>附上完整代码</p>
  20. <p></p>
  21. <p class="p1">AnimationPauseViewController.h</p>
  22. <pre name="code" class="cpp">#import <UIKit/UIKit.h>
  23. @interface AnimationPauseViewController : UIViewController {
  24. UIImageView *soccer;
  25. BOOL isPause;
  26. UIButton *controlButton;
  27. }
  28. @property (nonatomic, retain) IBOutlet UIImageView *soccer;
  29. - (IBAction)clickControlButton:(id)sender;
  30. @property (nonatomic, retain) IBOutlet UIButton *controlButton;
  31. @end</pre><br>
  32. <p></p>
  33. <p class="p1">AnimationPauseViewController.m</p>
  34. <p></p><pre name="code" class="cpp">#import "AnimationPauseViewController.h"
  35. #import <QuartzCore/QuartzCore.h>
  36. @implementation AnimationPauseViewController
  37. @synthesize controlButton;
  38. @synthesize soccer;
  39. - (void)dealloc
  40. {
  41. [soccer release];
  42. [controlButton release];
  43. [super dealloc];
  44. }
  45. - (void)didReceiveMemoryWarning
  46. {
  47. // Releases the view if it doesn't have a superview.
  48. [super didReceiveMemoryWarning];
  49. // Release any cached data, images, etc that aren't in use.
  50. }
  51. - (void)addAnimations
  52. {
  53. //让足球来回移动
  54. CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
  55. translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(24, 240)];
  56. translation.toValue = [NSValue valueWithCGPoint:CGPointMake(320- 24, 240)];
  57. translation.duration = 2;
  58. translation.repeatCount = HUGE_VALF;
  59. translation.autoreverses = YES;
  60. //让足球来回转动
  61. CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  62. //kCAMediaTimingFunctionLinear 表示时间方法为线性,使得足球匀速转动
  63. rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  64. rotation.toValue = [NSNumber numberWithFloat:4 * M_PI];
  65. rotation.duration = 2;
  66. rotation.repeatCount = HUGE_VALF;
  67. rotation.autoreverses = YES;
  68. [soccer.layer addAnimation:rotation forKey:@"rotation"];
  69. [soccer.layer addAnimation:translation forKey:@"translation"];
  70. }
  71. #pragma mark - View lifecycle
  72. - (void)viewDidLoad
  73. {
  74. [super viewDidLoad];
  75. [self addAnimations];
  76. }
  77. - (void)viewDidUnload
  78. {
  79. [self setSoccer:nil];
  80. [self setControlButton:nil];
  81. [super viewDidUnload];
  82. }
  83. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  84. {
  85. // Return YES for supported orientations
  86. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  87. }
  88. //暂停layer上面的动画
  89. - (void)pauseLayer:(CALayer*)layer
  90. {
  91. CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
  92. layer.speed = 0.0;
  93. layer.timeOffset = pausedTime;
  94. }
  95. //继续layer上面的动画
  96. - (void)resumeLayer:(CALayer*)layer
  97. {
  98. CFTimeInterval pausedTime = [layer timeOffset];
  99. layer.speed = 1.0;
  100. layer.timeOffset = 0.0;
  101. layer.beginTime = 0.0;
  102. CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
  103. layer.beginTime = timeSincePause;
  104. }
  105. - (void)pauseSoccer
  106. {
  107. isPause = YES;
  108. [controlButton setTitle:@"继续" forState:UIControlStateNormal];
  109. [self pauseLayer:soccer.layer];
  110. }
  111. - (void)resumeSoccer
  112. {
  113. isPause = NO;
  114. [controlButton setTitle:@"暂停" forState:UIControlStateNormal];
  115. [self resumeLayer:soccer.layer];
  116. }
  117. - (IBAction)clickControlButton:(id)sender {
  118. if (isPause) {
  119. [self resumeSoccer];
  120. }else{
  121. [self pauseSoccer];
  122. }
  123. }
  124. @end</pre><br>
  125. <br>
  126. <p></p>

在ios中运用core animation暂停和继续动画的更多相关文章

  1. 使用Core Animation对象来实现动画

    转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...

  2. iOS开发之Core Animation

    在IOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了. 在Core Animation中我们经常使用的是 CABasi ...

  3. iOS Instruments之Core Animation动画性能调优(工具复选框选项介绍)

    Core Animation工具用来监测Core Animation性能.它给我们提供了周期性的FPS,并且考虑到了发生在程序之外的动画(见图12.4) Core Animation工具提供了一系列复 ...

  4. iOS Core Animation学习总结(3)--动画的基本类型

    一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...

  5. Core Animation之CABasicAnimation(基础动画)

    #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UIButto ...

  6. core Animation之CAKeyframeAnimation(关键帧动画)

    CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...

  7. iOS开发基础知识:Core Animation(核心动画)

    Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core A ...

  8. iOS之核心动画(Core Animation)

      Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core ...

  9. iOS开发 - Core Animation 核心动画

    Core Animation Core Animation.中文翻译为核心动画,它是一组很强大的动画处理API,使用它能做出很炫丽的动画效果.并且往往是事半功倍. 也就是说,使用少量的代码就能够实现很 ...

随机推荐

  1. ffmpeg 复用

    aa 将mkv中的音视频复用成ts流: ffmpeg -i 32_mkv_h264_718x480_ac3.mkv  -codec copy -bsf:v h264_mp4toannexb  -f m ...

  2. 微信小程序页面跳转

    一:跳转的数据传递 例如:wxml中写了一个函数跳转: [html] view plain copy <view class="itemWeight" catchtap=&q ...

  3. MYSQL百万级数据,如何优化

    MYSQL百万级数据,如何优化     首先,数据量大的时候,应尽量避免全表扫描,应考虑在 where 及 order by 涉及的列上建立索引,建索引可以大大加快数据的检索速度.但是,有些情况索引是 ...

  4. PL/SQL 异常错误处理

     异常错误处理 一个优秀的程序都应该可以正确处理各种出错情况,并尽可能从错误中恢复.ORACLE 提供异常情况(EXCEPTION)和异常处理(EXCEPTION HANDLER)来实现错误处理 ...

  5. SVN学习(一)——SVN 检出文件步骤、图标显示及含义

    May, I come... 1. 创建一个目录用来存放检出得到的文件,例如MyCRM 2. 直接进入目录MyCRM,点右键 3. 可以看到检出得到的文件 此时文件图标上没有任何标识.可能你会想到通过 ...

  6. C语言学习笔记(五) 数组

    数组 数组的出现就是为了解决大量同类型数据的存储和使用的问题: 数组的分类:一维数组.二维数组. 一维数组:为多个变量连续分配存储控件:所有的变量的数据类型必须相同:所有变量所占的字节大小必须相等: ...

  7. web.py学习遇到的问题

    刚配置好了web.py运行所需要的环境,试着运行一个入门小实例,结果遇到了异常提示.不知道是什么原因导致的(是环境没配置好?还是……),暂时做个标记,记录一下. 运行的代码 import web ur ...

  8. 一步步教你如何进行Xilinx SerDes调试

    FPGA SERDES的应用需要考虑到板级硬件,SERDES参数和使用,应用协议等方面.由于这种复杂性,SERDES的调试工作对很多工程师来说是一个挑战.本文将描述SERDES的一般调试方法,便于工程 ...

  9. Backup and Recovery Basics1

    一.Backup and Recovery Overview 1.Backup and Recovery Overview 1.1 What is Backup and Recovery? 一般,备份 ...

  10. Python内置函数之ascii()

    ascii()返回一个字符串对象. ascii()的参数只能有一个. 如果参数中有非ascii字符,会用 \u,\U,\x 来替代. ascii()和Python2中repr()等效 下面看看例子: ...