0

Normally I don't perform any animation in viewDidLoad.

But for testing purposes, I tried to place and test these one by one within viewDidLoad:


CABasicAnimation

[UIView animateWithDuration:delay:options:animations:completion:]

and finally the now deprecated:

[UIView BeginAnimation]

(which I haven't used for a long time; again, just for testing purpose in this case)


And I experienced some thing I cannot explain for the past hours.

In viewDidLoad, if I start the CABasicAnimation, it works - it animates.

But if I place either:

[UIView animateWithDuration:delay:options:animations:completion:] or [UIView BeginAnimation] in viewDidLoad, no animation is started and instead the item will be placed at the final animated position.

Out of curiosity, I did the following:

I wrapped either [UIView animateWithDuration:delay:options:animations:completion:] or[UIView BeginAnimation] within:

double delayInSeconds = 0.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
    //UIView animation block / [CAAnimation Begin] Animation goes here. As you can see,   
    //there is no delay here.
    //I did this out of hunches.
});

again in viewDidLoad, then animation happens. It is not because of dispatch_get_main_queue() I think because I tried to log the queue before dispatch_after above by using:

NSLog(@"%@", [NSOperationQueue mainQueue]);

It is in fact on the main queue (as a matter of fact, I am not using any GCD at all. But just to confirm, I tried to log it this way.

Can anyone explain to me why CABasicAnimation works here in viewDidLoad and others don't. And why placing UIView animation block and the [CAAnimation Begin] within dispatch_after even without delay works?

I am just testing these types of animation methods (that is my goal); so I am not doing complex animation - it is just something as simple as moving an item linearly from one point to another.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Is is possible that the `UIView` methods just ignore your efforts to animate if the view has no superview? – Mike Pollard Feb 10 '14 at 13:33
  • @MikePollard Can you elaborate a bit more? Besides I am still baffled at why it would work within the `dispatch_after` block. Confirmed: `[self.view superview];` returns null in `viewDidLoad`. – Unheilig Feb 10 '14 at 13:37
  • Check if `[self.view superview]` returns nil within your `dispath_after` block? – Mike Pollard Feb 10 '14 at 13:41
  • @MikePollard Both before and after returns nil. But within `dispatch_after` it is not nil. Interesting. Note: it returns nil, nil first before the `[self.view superview];` within `dispatch_after` is logged, even though `[self.view superview];` is placed between the two. – Unheilig Feb 10 '14 at 13:46
  • Please check http://stackoverflow.com/questions/13065593/cabasicanimation-is-not-working-when-the-method-is-called-from-the-viewdidload – Midhun MP Feb 10 '14 at 13:56
  • @MidhunMP That doesn't answer my question at all - `CABasicAnimation` is **working** here, whereas within that it doesn't. Besides there is more context in my question here than just that within that question - namely why does some animations work within `dispatch_after`. They are different entirely. – Unheilig Feb 10 '14 at 13:59
  • There you go. When you use `dispatch_after` the block is not run immediately (even though the delay in 0.0). In the mean time the view has been added to the hierarchy. Essentially this show why you shouldn't be initiating animations in `viewDidLoad`. – Mike Pollard Feb 10 '14 at 15:03
  • @MikePollard Oh, so there is actually a delay in `dispatch_after` even though the delay is 0.0? How could we prove that? – Unheilig Feb 10 '14 at 15:06
  • 1
    Well, dispatch_after adds the block to the mainQueue which is currently busy in your viewDidLoad method. It'll get executed at best immediately after the current event is done. – Mike Pollard Feb 10 '14 at 15:11
  • @MikePollard Thanks. Can you please sum that up as an answer so I can upvote you at least? Yours is a good answer here, but I am really eager to know if what we enqueue is in fact not executed immediately and I would like to know the timing of its execution, because if they don't get executed immediately even if we want them to, what else is there for us to ensure things do get executed as we program them to? – Unheilig Feb 10 '14 at 15:19

0 Answers0