Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView Animation at viewDidLoad/viewDidAppear?

I am attempting a simple UIView animation in the viewDidLoad or viewDidAppear method but it doesn't animate.

UIImage *bluredScreenshot = [self.parentViewControllerScreenshot applyBlur];

    [UIView transitionWithView:self.screenshotView duration:3.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.screenshotView.image = bluredScreenshot;
    } completion:nil];

Simple cross dissolving two images. When ran from viewDidLoad or viewDidAppear the image changes but isn't animated.

But lets say I run the animation from a method after I press a button, it will animate. Why? Seem strange.

Is it possible to make it from the viewDidLoad method? How would you solve this?

Thanks.

like image 466
Josh Kahane Avatar asked Dec 04 '25 14:12

Josh Kahane


2 Answers

You need to fade in the screenshot. If you're using two images, you'll need to place one image view atop the other. pop this inside viewDidAppear: after calling [super viewDidAppear:animated];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.screenshotView.frame];
[self.view addSubview:imageView];
    [imageView setAlpha:0.0f];
    imageView = blurredScreenshot;
    [UIView animateWithDuration:0.3f animations:^{
                [imageView setAlpha:1.0f];
            } completion:^(BOOL finished){
                self.imageView.image = blurredScreenshot;
                [imageView removeFromSuperview];
}];
like image 76
David Wong Avatar answered Dec 06 '25 05:12

David Wong


when the view is loading you can't animate because there isn't anything to animate. Try in viewdidapear: and Don't use transition

[UIView animateWithDuration:3.
                      delay:0
                    options:UIViewAnimationOptionTransitionCrossDissolve
                 animations:^{
                             self.screenshotView.image = bluredScreenshot;
                 }
                 completion:nil];
like image 28
Radu Avatar answered Dec 06 '25 03:12

Radu