xcode 如何在 viewDidAppear 中为 UIView 添加动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:06:12  来源:igfitidea点击:

How to add an animation to the UIView in viewDidAppear?

iphonexcodeuiviewviewdidloadviewdidappear

提问by Flocked

I tried to add a animation to viewDidLoad and viewDidAppear, but it doesn't work:

我试图向 viewDidLoad 和 viewDidAppear 添加动画,但它不起作用:

- (void)viewDidAppear:(BOOL)animated{
 [UIView beginAnimations:@"transition" context:NULL];
 [UIView setAnimationTransition:110 forView:self.view cache:YES];
 [UIView commitAnimations];
}

Why?

为什么?

回答by Hymanbravo

I had the same problem and I think I found the solution on this SO question.

我遇到了同样的问题,我想我在这个SO question上找到了解决方案。

When viewDidAppear gets called you still don't see anything on the screen (despite the name), but you are about to. You can then use a performSelector:withDelay or an NSTimer to launch your animation. The delay can just be 0.1 and your animation will play just when the screen appears.

当 viewDidAppear 被调用时,您仍然看不到屏幕上的任何内容(尽管有名称),但您即将看到。然后您可以使用 performSelector:withDelay 或 NSTimer 来启动您的动画。延迟可以是 0.1,您的动画将在屏幕出现时播放。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

- (void)animationCode {
    // you animation code
}

回答by Ole Begemann

You are not telling the view which state it should animate to so it won't do anything. You need to place code between beginAnimations:context:and commitAnimationsthat changes the appearance of the view (e.g. by removing one subview and adding another).

你没有告诉视图它应该动画到哪个状态,所以它不会做任何事情。您需要在beginAnimations:context:和之间放置代码commitAnimations以更改视图的外观(例如,通过删除一个子视图并添加另一个子视图)。

回答by lawrence

  1. You're not using beginAnimations:and commitAnimationscorrectly. You're supposed to put something in between them that normally wouldn't be animated: e.g. with self.view.alpha = 0.5you get a fading effect. They have no effect on anything that isn't between them.

  2. By the time viewDidAppear:is called, your view, well... has appeared. It's too late to animate anything. What you actually want to do is something like this:

    - (void)showMyViewWithAnimation {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:110 forView:childView cache:YES];
        [parentView addSubview:childView];
        [UIView commitAnimations];
    }
    

    In the example above, childViewis what in your example is called self.view.

  3. Please write out the name of the transition; no one knows what 110 is by looking at it. It's bad style. </pedantry>

  1. 您没有正确使用beginAnimations:commitAnimations。你应该在它们之间放置一些通常不会被动画化的东西:例如,self.view.alpha = 0.5你会得到一个淡入淡出的效果。他们对不在他们之间的任何事情都没有影响。

  2. 到时候viewDidAppear:,你的观点,嗯……已经出现了。现在为任何东西制作动画都为时已晚。你真正想做的是这样的:

    - (void)showMyViewWithAnimation {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:110 forView:childView cache:YES];
        [parentView addSubview:childView];
        [UIView commitAnimations];
    }
    

    在上面的例子中,childView你的例子中被称为self.view.

  3. 请写出过渡的名称;没有人一看就知道110是什么。画风很差 </pedantry>