AnimationPause
@implementation AnimationViewController
@synthesize controlButton;
@synthesize image;
- (void)dealloc
{
[image 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.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(3.1415, 0, 0, 1.0)];//翻转效果
// translation.toValue = [NSNumber numberWithDouble:1.5];//缩放效果
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;
[image.layer addAnimation:rotation forKey:@"rotation"];
[image.layer addAnimation:translation forKey:@"translation"];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self addAnimations];
}
- (void)viewDidUnload
{
[self setImage: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:@"continue" forState:UIControlStateNormal];
[self pauseLayer:image.layer];
}
- (void)resumeSoccer
{
isPause = NO;
[controlButton setTitle:@"pause" forState:UIControlStateNormal];
[self resumeLayer:image.layer];
}
- (IBAction)clickControlButton:(id)sender {
if (isPause) {
[self resumeSoccer];
}else{
[self pauseSoccer];
}
}
@end
原文地址 : http://blog.sina.com.cn/s/blog_690f60a401015fjd.html