在ios中运用core animation暂停和继续动画
本文转载至 http://blog.csdn.net/wildfireli/article/details/23191861
暂停和继续动画的核心代码如下:
- <pre name="code" class="cpp">//暂停layer上面的动画
- - (void)pauseLayer:(CALayer*)layer
- {
- CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
- layer.speed = 0.0;
- layer.timeOffset = pausedTime;
- }
- //继续layer上面的动画
- - (void)resumeLayer:(CALayer*)layer
- {
- CFTimeInterval pausedTime = [layer timeOffset];
- layer.speed = 1.0;
- layer.timeOffset = 0.0;
- layer.beginTime = 0.0;
- CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
- layer.beginTime = timeSincePause;
- }</pre><br>
- <pre></pre>
- <p>附上完整代码</p>
- <p></p>
- <p class="p1">AnimationPauseViewController.h</p>
- <pre name="code" class="cpp">#import <UIKit/UIKit.h>
- @interface AnimationPauseViewController : UIViewController {
- UIImageView *soccer;
- BOOL isPause;
- UIButton *controlButton;
- }
- @property (nonatomic, retain) IBOutlet UIImageView *soccer;
- - (IBAction)clickControlButton:(id)sender;
- @property (nonatomic, retain) IBOutlet UIButton *controlButton;
- @end</pre><br>
- <p></p>
- <p class="p1">AnimationPauseViewController.m</p>
- <p></p><pre name="code" class="cpp">#import "AnimationPauseViewController.h"
- #import <QuartzCore/QuartzCore.h>
- @implementation AnimationPauseViewController
- @synthesize controlButton;
- @synthesize soccer;
- - (void)dealloc
- {
- [soccer release];
- [controlButton release];
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)addAnimations
- {
- //让足球来回移动
- CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"];
- translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(24, 240)];
- translation.toValue = [NSValue valueWithCGPoint:CGPointMake(320- 24, 240)];
- translation.duration = 2;
- translation.repeatCount = HUGE_VALF;
- translation.autoreverses = YES;
- //让足球来回转动
- CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- //kCAMediaTimingFunctionLinear 表示时间方法为线性,使得足球匀速转动
- rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
- rotation.toValue = [NSNumber numberWithFloat:4 * M_PI];
- rotation.duration = 2;
- rotation.repeatCount = HUGE_VALF;
- rotation.autoreverses = YES;
- [soccer.layer addAnimation:rotation forKey:@"rotation"];
- [soccer.layer addAnimation:translation forKey:@"translation"];
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self addAnimations];
- }
- - (void)viewDidUnload
- {
- [self setSoccer:nil];
- [self setControlButton:nil];
- [super viewDidUnload];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- //暂停layer上面的动画
- - (void)pauseLayer:(CALayer*)layer
- {
- CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
- layer.speed = 0.0;
- layer.timeOffset = pausedTime;
- }
- //继续layer上面的动画
- - (void)resumeLayer:(CALayer*)layer
- {
- CFTimeInterval pausedTime = [layer timeOffset];
- layer.speed = 1.0;
- layer.timeOffset = 0.0;
- layer.beginTime = 0.0;
- CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
- layer.beginTime = timeSincePause;
- }
- - (void)pauseSoccer
- {
- isPause = YES;
- [controlButton setTitle:@"继续" forState:UIControlStateNormal];
- [self pauseLayer:soccer.layer];
- }
- - (void)resumeSoccer
- {
- isPause = NO;
- [controlButton setTitle:@"暂停" forState:UIControlStateNormal];
- [self resumeLayer:soccer.layer];
- }
- - (IBAction)clickControlButton:(id)sender {
- if (isPause) {
- [self resumeSoccer];
- }else{
- [self pauseSoccer];
- }
- }
- @end</pre><br>
- <br>
- <p></p>
在ios中运用core animation暂停和继续动画的更多相关文章
- 使用Core Animation对象来实现动画
转载保留原文地址:http://blog.csdn.net/kqjob/article/details/10417461,转载的 在iOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现, ...
- iOS开发之Core Animation
在IOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了. 在Core Animation中我们经常使用的是 CABasi ...
- iOS Instruments之Core Animation动画性能调优(工具复选框选项介绍)
Core Animation工具用来监测Core Animation性能.它给我们提供了周期性的FPS,并且考虑到了发生在程序之外的动画(见图12.4) Core Animation工具提供了一系列复 ...
- iOS Core Animation学习总结(3)--动画的基本类型
一. CABasicAnimation (基础动画) 移位: CABasicAnimation *animation = [CABasicAnimation animation]; //keyPath ...
- Core Animation之CABasicAnimation(基础动画)
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UIButto ...
- core Animation之CAKeyframeAnimation(关键帧动画)
CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSA ...
- iOS开发基础知识:Core Animation(核心动画)
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core A ...
- iOS之核心动画(Core Animation)
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core ...
- iOS开发 - Core Animation 核心动画
Core Animation Core Animation.中文翻译为核心动画,它是一组很强大的动画处理API,使用它能做出很炫丽的动画效果.并且往往是事半功倍. 也就是说,使用少量的代码就能够实现很 ...
随机推荐
- Python连接MySQL乱码(中文变问号)
#coding=utf-8 import MySQLdb db = MySQLdb.connect("IP","用户名","密码",&quo ...
- go get 下载的包放在哪里呢?
有些问题,我以前都是似懂非懂,没有去弄个究竟!!!!! 这个习惯非常不好,搞得有些东西看似懂了,又不能百分之百说自己懂了,可能下次就弄不出来了,这样是不可取的. 不能有这种做事的风格. ------- ...
- 转: Linux与JVM的内存关系分析
Linux与JVM的内存关系分析 引言 在一些物理内存为8g的服务器上,主要运行一个Java服务,系统内存分配如下:Java服务的JVM堆大小设置为6g,一个监控进程占用大约600m,Linux自身使 ...
- Wix 安装部署教程 -CustomAction的七种用法
在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...
- mui 根据 json 数据动态创建列表
使用 underscore.js 模块解析 Underscore提供了一个轻量级的模板解析函数,它可以帮助我们有效地组织页面结构和逻辑. 实例: <!DOCTYPE html> <h ...
- javascript入门系列演示·三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- Linux /bin, /sbin, /usr/bin, /usr/sbin 区别(转)
在linux下我们经常用到的四个应用程序的目录是:/bin./sbin./usr/bin./usr/sbin bin: bin为binary的简写主要放置一些系统的必备执行档例如:cat.cp ...
- svn上检出的项目在myeclipse中报错的解决
项目本身应该是没问题的,应该是验证方面导致的错误的报错,取消验证即可:右键项目,如下操作: 除此之外,还应考虑是否环境问题引起的报错,比如:项目jar包是否发布,jdk的版本,tomcat的版本,等等
- 如何在谷歌浏览器中查看Axure做的原型HTML
Axure RP Pro可谓是非常方便.好用的一款软件,因为它不仅能绘制出详细的产品构思,也能生成浏览器格式的产品原型.但如果想把原型拿给客户查看,需要看产品画的原型图时,但是打不开的话就悲剧了.经常 ...
- partial(类型)(C# 参考)
通过分部类型可以定义要拆分到多个文件中的类.结构或接口. 如下: 在 File1.cs 中: namespace PC { partial class A { ; void MethodA() { } ...