在viewDidLoad/viewDidAppear中使用UIView动画?

3

我试图在viewDidLoad或viewDidAppear方法中进行简单的UIView动画,但它没有动起来。

UIImage *bluredScreenshot = [self.parentViewControllerScreenshot applyBlur];

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

简单的交叉溶解两个图像。当从viewDidLoad或viewDidAppear运行时,图像会更改但不会动画。

但是,如果我在按下按钮后从方法运行动画,则会执行动画。为什么?看起来很奇怪。

是否可能从viewDidLoad方法中完成它?你会如何解决这个问题?

谢谢。


你为什么要在 viewDidLoad 中执行这个操作呢?视图还没有显示出来。应该在 viewDidAppear: 或者可能是 viewWillAppear: 中执行动画。 - rmaddy
我已经尝试在viewDidAppear中,但效果相同。 - Josh Kahane
3个回答

3

当视图正在加载时,因为没有任何内容可以进行动画,所以无法执行动画。请在 viewdidapear: 中尝试,并且不要使用转场。

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

对我来说,解决了一个问题,即viewDidLoad不会执行动画。 - Darius Miliauskas

3

您需要淡入截图。如果使用两张图片,则需要将一个图像视图放在另一个上面。 在调用[super viewDidAppear:animated];之后,在viewDidAppear:方法中添加此代码。

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];
}];

0

提醒一下,有时候你的问题可能是由于没有调用引起的:

self.layoutIfNeeded()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接