动画组CAAnimationGroup的使用十分简单,核心想法是把多个动画效果组合起来,各个动画效果并行处理,这些动画效果可以是CABasicAnimation,也可以是CAKeyframeAnimation

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(0, 0, 120, 120);
    layer.position = CGPointMake(60, 100);
    layer.cornerRadius = 60;
    layer.masksToBounds = YES;
    layer.contents = (id)[UIImage imageNamed:@"5.png"].CGImage;
    [self.view.layer addSublayer:layer];

}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    CALayer *layer = [self.view.layer.sublayers lastObject];

    
    //自转动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue = @M_PI;
    animation.autoreverses = YES;
    animation.duration = 1;
    
    
    //绕矩阵旋转动画
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGPathRef path = CGPathCreateMutable();
    CGPathAddRect(path, &CGAffineTransformIdentity, CGRectMake(layer.position.x, layer.position.y, 200, 300));
    keyFrameAnimation.path = path;
    keyFrameAnimation.duration = 2;
    CGPathRelease(path);
    
    //创建动画组,包含前面两种动画
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.animations = @[animation,keyFrameAnimation];
    animationGroup.duration = 2;
    animationGroup.repeatCount = HUGE_VALF;
    
    [layer addAnimation:animationGroup forKey:nil];
    
    
}

 上面代码实现的效果是图片边自转边绕矩形路径转圈

posted on 2014-10-24 10:39  幸福小弥  阅读(497)  评论(0编辑  收藏  举报