10

I have a ViewController with a bunch of UIImageViews that I need to send to the front and animate to the center of the view.

I have tried to call bringSubviewToFront before casting the UIView animation and what seems to be happening is that the view is sent to the top of the stack, but the animation does not happen (even though the completion handler log shows as if the animation DID happen).

The second time i press the button that triggers the animation (considering that this time the view is already at top index, the animation takes place as it should)

Heres my code:

- (void)beginAnimationWithSelectedCountry:(UIView *)countryView
{
    if (!countryView) return;

    [self.view bringSubviewToFront:countryView];
    [UIView animateWithDuration:0.6f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        countryView.center = CGPointMake(self.view.center.x, self.view.center.y);
    } completion:^(BOOL finished) {
        NSLog(@"Animation complete");
    }];
}

Any thoughts? I've been at this for hours >.<

Thanks

EDIT: If I comment out the bringSubviewToFront the code executes flawlessly with the animation (except the view is under some other views) so i'm preety sure the problem is with the line:

[self.view bringSubviewToFront:countryView];

Heres a screenshot of what I have currently that may shed some more light on the issue: http://d.pr/i/PKoa

Salvador Mosti
  • 171
  • 2
  • 5
  • I would start with that if- return statement there and see if your method is even being called the first time around with an NSLog. – CodaFi Oct 17 '12 at 20:11
  • Already checked, as I posted the "Animation complete" log is being called both times. Thanks though – Salvador Mosti Oct 17 '12 at 20:25
  • 1
    do this - put the animation in a second method called animateCountry put the animation code in it - in the begin method call bringSubViewToFront - then call [self performSelector:animateCountry... it will work. – deleted_user Oct 22 '12 at 21:34
  • 1
    have you found solution to it? – turtle Mar 21 '13 at 06:19
  • What is the solution for this? – Mrunal Mar 21 '13 at 14:06
  • as http://stackoverflow.com/a/10038688/1097127 said, bringSubViewToFront will cause superview's layoutSubview called. I use zPosition fix my problem. Before this, I use @deleted_user 's way can work out for a moment, and then I found it still not perfect. I think **zPosition** is the right way. – ZhouQi Nov 12 '13 at 11:43
  • make sure if you are using a .nib you don't have any "autolayouts" in there breaking shit – anders Apr 22 '14 at 20:41

3 Answers3

6

Call:

[self.view layoutSubviews];

Right after:

[self.view bringSubviewToFront:countryView];

It will solve your problem.

yogevbd
  • 1,548
  • 1
  • 14
  • 18
3

When you call

[self.view bringSubviewToFront:countryView];

you trigger a new view layout event. You can check this by setting a breakpoint in 'viewDidLayoutSubviews' on your controller.

I had a similar issue: my own code in the 'viewDidLayoutSubviews' method cancelled my animation by resetting the frames of the objects I was trying to animate.

Barry Haanstra
  • 253
  • 2
  • 10
  • 2
    Hi Barry, thanks for letting us know what was causing it to fail. Do you know of anyway to get around this issue? – Jon Cox Dec 19 '12 at 20:23
0

Try adding UIViewAnimationOptionAllowAnimatedContent to the options. I am almost sure this is a case of this option, but play with the other options if not. You can also try scheduling a block to execute your animation on the next runloop, which is a blessing, I have found, with animations.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195