Implicit property animations do not work with CARe

2019-07-13 10:10发布

问题:

The sample code is here.

After replacing explicit property animations with implicit property animations, the animation is broken.

Explicit animation:

-(void)animate:(id)sender {
    ...
    //Transform Animation
    animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D: CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: t];
    animation.duration = 1.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [subLayer addAnimation:animation forKey:@"transform"];

    //Opacity Animation
    animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.toValue = [NSNumber numberWithFloat:0.0];
    animation.duration = 1.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [subLayer addAnimation:animation forKey:@"opacity"];
    ...
}

-(void)reset:(id)sender {       
    ...
    //Transform Animation
    animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D: t];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DIdentity];
    animation.duration = 1.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [subLayer addAnimation:animation forKey:@"transform"];

    //Opacity Animation
    animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:1.0];
    animation.duration = 1.0;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    [subLayer addAnimation:animation forKey:@"opacity"];

    ...
}

Implicit animation:

-(void)animate:(id)sender {
    ...
    //Transform Animation
    [CATransaction setAnimationDuration:1];
    subLayer.transform = t;

    //Opacity Animation
    [CATransaction setAnimationDuration:1];
    subLayer.opacity = 0;
    ...
}

-(void)reset:(id)sender {       
    ...
    //Transform Animation
    [CATransaction setAnimationDuration:1];
    subLayer.transform = CATransform3DIdentity;

    //Opacity Animation
    [CATransaction setAnimationDuration:1];
    subLayer.opacity = 1;       
    ...
}

Why?

回答1:

u don't need to use CATrasaction when you use implicit animation. be carefull that uikit disables the implicit animation of layer which is a root layer of UIView



回答2:

You should to set CALayer's delegate to something different that view controllers view at an appropriate time (none of nitWithNibName:bundle:, awakeFromNib, viewDidLoad, and viewWillAppear:animated), look here: Does iPhone OS support implicit animation? .

On my machine calling animate on a touch worked very nicely.