简体   繁体   English

在viewDidLoad / viewDidAppear的UIView动画?

[英]UIView Animation at viewDidLoad/viewDidAppear?

I am attempting a simple UIView animation in the viewDidLoad or viewDidAppear method but it doesn't animate. 我正在viewDidLoad或viewDidAppear方法中尝试一个简单的UIView动画,但它没有动画。

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. 从viewDidLoad或viewDidAppear运行时,图像会更改但不会设置动画。

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? 是否可以从viewDidLoad方法中创建它? How would you solve this? 你怎么解决这个问题?

Thanks. 谢谢。

when the view is loading you can't animate because there isn't anything to animate. 当视图加载时,您无法设置动画,因为没有任何动画效果。 Try in viewdidapear: and Don't use transition 尝试在viewdidapear:并且不要使用过渡

[UIView animateWithDuration:3.
                      delay:0
                    options:UIViewAnimationOptionTransitionCrossDissolve
                 animations:^{
                             self.screenshotView.image = bluredScreenshot;
                 }
                 completion:nil];

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]; 在viewDidAppear内部弹出:调用[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];
}];

只是提醒一下,有时你的问题可能是由于没有打电话引起的:

self.layoutIfNeeded()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM