2

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?

an0
  • 17,191
  • 12
  • 86
  • 136

2 Answers2

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

wihing
  • 541
  • 4
  • 9
  • [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:10.0f] forKey:kCATransactionAnimationDuration]; theLayer.zPosition=200.0; theLayer.opacity=0.0; [CATransaction commit]; – wihing Feb 09 '12 at 15:33
-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.

Community
  • 1
  • 1
Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32
  • Obviously, I'm using implicit transactions. – an0 Jul 07 '11 at 18:30
  • Obviously, I was wrong, did find a hint but couldn't test b/c of no OSX at hand. Obviously, you don't know the answer b/c you otherwise wouldn't ask. Now I've researched a bit more and found an answer that worked on my machine. – Tomasz Stanczak Jul 07 '11 at 19:37
  • BTW CATransaction begin/commit don't make an animation explicit, as long as you won't create an animation and add it to the layer it still is an implicit animation. Obviously, I should add :-) – Tomasz Stanczak Jul 07 '11 at 19:47