在ios中运用core animation暂停和继续动画
2016-03-13 13:42
477 查看
<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>
- (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详解
- 【iOS界面开发】视图开发技巧
- HDU A Mathematical Curiosity
- iOS 8推送注册方式改变的问题
- 检查iOS项目中是否使用了IDFA
- iOS开发笔记--什么时候调用layoutSubviews
- ios-提升之【6】-数据持久化
- iOS didMoveToSuperview
- ios-基础之【9】-NSNumber,NSDate,NSExcetion
- [iOS]iOS GitHub Top 100 简介
- IOS开发之多线程 -- GCD的方方面面
- Github上600多个iOS开源项目分类及介绍
- ios-基础之【8】-NSDictionary
- ios-基础之【7】-NSArrary
- iOS GitHub Top 100 简介
- ios-基础之【6】-NSString
- iOS开发------响应TableView下拉设置NavigationBar的透明度
- iOS开发证书都显示“此证书的签发者无效”,更新WWDR Certificate证书后还是显示无效
- iOS边练边学--介绍布局的三种方法
- iOS GitHub Top 100 简介