0

I have the following code to animate a layer in viewDidLoad in my view controller. Basically, I want the animation to show when the view is loaded.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView* mainview = self.view;
    CompassLayer* cl = [CompassLayer new];
    cl.frame = CGRectMake(30, 30, 200, 200);
    [cl assembleSublayers];
    [mainview.layer addSublayer:cl];

    CAMediaTimingFunction* clunk = [CAMediaTimingFunction functionWithControlPoints:.9 :.1 :.7 :.9];
    [CATransaction setAnimationDuration:0.8];
    [CATransaction setAnimationTimingFunction:clunk];
    cl.arrow.transform = CATransform3DRotate(cl.arrow.transform, M_PI/4.0, 0, 0, 1);

//    CATransition* t = [CATransition animation];
//    t.type = kCATransitionPush;
//    t.subtype = kCATransitionFromBottom;
//    [cl addAnimation:t forKey:nil];

}

The commented out transition animation works, but not the layer transform ones above. Can someone explain to me why?

Basically, the layer that I am trying to animate comes out in its final state.

Thank you.

kev
  • 1,085
  • 2
  • 10
  • 27

2 Answers2

0

This is not what viewDidLoad is for. This method is for doing additional setup after the ViewController loads the view hierarchy. The view is not going to be visible on the screen when viewDidLoad is called so you're not going to see the animation.

Try moving your code to viewDidAppear:.

Rob Jones
  • 4,925
  • 3
  • 32
  • 39
  • Thanks for your reply. The animation still did not appear even when I've moved it to viewDidAppear. Do you know if there is anything wrong with the animation code? – kev Mar 19 '14 at 06:54
  • Yeah. It looks like the rest of the answer is in jrturton's answer. – Rob Jones Mar 19 '14 at 16:27
0

As I understand it, implicit animations are disabled on iOS unless you are within a UIView animation block - see here for more discussion.

You need to explicitly begin and commit a CATransaction to see this animation.

I agree with the other answer that it shouldn't be in viewDidLoad - you've got no guarantee the view has been added to your window at that point, viewDidAppear is a better place.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268