关于CABasicAnimation —实现简单的滚动、平移、翻转、缩放动画

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值