iOS动画之-动画组

动画组

CAAnimationGroup用于添加多个动画,可以将平移、缩放、旋转等乖动画添加到一个动画组,实现更炫的效果。相关代码如下所示

    CABasicAnimation *transformX = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    transformX.toValue = @(100);
    transformX.removedOnCompletion = NO;//是否返回到初始状态
    transformX.fillMode = kCAFillModeForwards;//保持结束后的状态
    
    CABasicAnimation *transformScale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    transformScale.toValue = @(1.3);
    transformScale.removedOnCompletion = NO;//是否返回到初始状态
    transformScale.fillMode = kCAFillModeForwards;//保持结束后的状态
    [self.animationView addAnimation:transformX,transformScale,nil];
}

封装在分类中的代码如下所示:

-(void)addAnimation:(CAAnimation*) animations, ... NS_REQUIRES_NIL_TERMINATION{
    va_list arg_list;
    NSMutableArray *array = [[NSMutableArray alloc]init];
    if(animations){
        [array addObject:animations];
        va_start(arg_list, animations);
        CAAnimation *animation = va_arg(arg_list, CAAnimation *);
        while(animation){
            [array addObject:animation];
            animation = va_arg(arg_list, CAAnimation *);
        }
        va_end(arg_list);
        CAAnimationGroup *groupAnimation =  [CAAnimationGroup animation];
        groupAnimation.animations = array;
        groupAnimation.duration = ML_ANIMATION_DURATION;
        [self.layer addAnimation:groupAnimation forKey:@"groupAnimation"];
    }
}

demo
个人博客

你可能感兴趣的:(iOS动画之-动画组)