Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS bringSubviewToFront blocking UIView animation

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

like image 618
Salvador Mosti Avatar asked Oct 17 '12 19:10

Salvador Mosti


2 Answers

Call:

[self.view layoutSubviews];

Right after:

[self.view bringSubviewToFront:countryView];

It will solve your problem.

like image 104
yogevbd Avatar answered Nov 19 '22 01:11

yogevbd


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.

like image 3
Barry Haanstra Avatar answered Nov 18 '22 23:11

Barry Haanstra