Core Animation如何使显式动画结束时的值直接作用Layer

(1)使用隐式动画会直接改变layer的属性值,如:

imageView.layer.opacity = 0.3;

(2)使用显式动画,动画结束时不影响动画前的layer属性值,如:

    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];

    opacityAnim.toValue = [NSNumber numberWithFloat:0.3];

    opacityAnim.removedOnCompletion = YES;

    opacityAnim.duration = 2;

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

这里的动画结束后,Layer的opacity的值仍为原值:1.0

 

(3)如果想显式动画结束时,其值为动画结束时的值,需要在显式动画后加上隐式动画代码来真正更改layer属性值,如:

 

    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];

    opacityAnim.toValue = [NSNumber numberWithFloat:0.3];

    opacityAnim.removedOnCompletion = YES;

    opacityAnim.duration = 2;

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

    imageView.layer.opacity = 0.3;

 

 

 

----------------------------------------------------------------------------

----------------------------------------------------------------------------

// 代码备忘

 

    CGPoint fromPoint = imageView.center;

    UIBezierPath *movePath = [UIBezierPath bezierPath];

    [movePath moveToPoint:fromPoint];

     CGPoint toPoint = CGPointMake(240, 310);//position

    [movePath addQuadCurveToPoint:toPoint

                     controlPoint:CGPointMake(toPoint.x,fromPoint.y)];

 

 

 

    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    moveAnim.path = movePath.CGPath;

    moveAnim.removedOnCompletion = YES;

 

    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];

    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];

    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)];

    scaleAnim.removedOnCompletion = YES;

 

    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];

    opacityAnim.toValue = [NSNumber numberWithFloat:0.5];

    opacityAnim.removedOnCompletion = YES;

 

    CAAnimationGroup *animGroup = [CAAnimationGroup animation];

    animGroup.animations = [NSArray arrayWithObjects:moveAnim,scaleAnim,opacityAnim, nil];

    animGroup.duration = 1;

    animGroup.removedOnCompletion = YES;

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

 

    self.imageView.layer.opacity = 0.5;

    self.imageView.layer.transform = CATransform3DMakeScale(0.5, 0.5, 1.0);

    self.imageView.layer.position = CGPointMake(240, 310);

猜你喜欢

转载自janedoneway.iteye.com/blog/1615636