CoreAnimation的使用

时间:2022-06-01 23:16:00

一、CABasicAnimation

    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position";
    //    anim.keyPath = @"transform.translation.x";
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(, )];
    anim.duration = 2.0;
    // 动画完成后不要删除动画
    anim.removedOnCompletion = NO;
    // 保持最新的状态
    anim.fillMode = kCAFillModeForwards;
    [myLayer addAnimation:anim forKey:nil];

二、CAKeyframeAnimation

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    anim.keyPath = @"position";
    //创建动画执行路径
    CGMutablePathRef path = CGPathCreateMutable();
    //路径属性
    CGPathAddEllipseInRect(path, NULL, CGRectMake(, , , ));
    anim.path = path;
    anim.duration = 3.0;
    anim.delegate = self;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    [myLayer addAnimation:anim forKey:nil];

三、CAAnimationGroup

    // 1.创建旋转动画对象
    CABasicAnimation *rotate = [CABasicAnimation animation];
    rotate.keyPath = @"transform.rotation";
    rotate.toValue = @(M_PI);

    // 2.创建缩放动画对象
    CABasicAnimation *scale = [CABasicAnimation animation];
    scale.keyPath = @"transform.scale";
    scale.toValue = @(0.0);

    // 3.平移动画
    CABasicAnimation *move = [CABasicAnimation animation];
    move.keyPath = @"transform.translation";
    move.toValue = [NSValue valueWithCGPoint:CGPointMake(, )];

    // 4.将所有的动画添加到动画组中
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[rotate, scale, move];
    group.duration = 2.0;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;

    [imageView.layer addAnimation:group forKey:nil];