如何在viewDidAppear中给UIView添加动画?
我尝试向 viewDidLoad 和 viewDidAppear 添加动画,但它不起作用:
- (void)viewDidAppear:(BOOL)animated{
[UIView beginAnimations:@"transition" context:NULL];
[UIView setAnimationTransition:110 forView:self.view cache:YES];
[UIView commitAnimations];
}
为什么?
I tried to add a animation to viewDidLoad and viewDidAppear, but it doesn't work:
- (void)viewDidAppear:(BOOL)animated{
[UIView beginAnimations:@"transition" context:NULL];
[UIView setAnimationTransition:110 forView:self.view cache:YES];
[UIView commitAnimations];
}
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,我想我在这个 SO 问题上找到了解决方案。
当 viewDidAppear 被调用时,您仍然在屏幕上看不到任何内容(尽管有名称),但您即将看到。然后,您可以使用 PerformSelector:withDelay 或 NSTimer 来启动动画。延迟可以仅为 0.1,并且动画将在屏幕出现时播放。
I had the same problem and I think I found the solution on this 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.
您没有告诉视图它应该动画到哪个状态,因此它不会执行任何操作。您需要在
beginAnimations:context:
和commitAnimations
之间放置代码来更改视图的外观(例如,通过删除一个子视图并添加另一个子视图)。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:
andcommitAnimations
that changes the appearance of the view (e.g. by removing one subview and adding another).您没有正确使用
beginAnimations:
和commitAnimations
。你应该在它们之间放置一些通常不会动画的东西:例如使用self.view.alpha = 0.5
你会得到一个淡入淡出的效果。 它们对它们之间以外的任何内容都没有影响。当
viewDidAppear:
被调用时,你的视图,嗯...已经出现了。现在对任何东西进行动画处理都为时已晚。你真正想做的是这样的:在上面的示例中,
childView
在您的示例中被称为self.view
。请写出转换的名称;没有人一看就知道110是什么。这是糟糕的风格。
You're not using
beginAnimations:
andcommitAnimations
correctly. You're supposed to put something in between them that normally wouldn't be animated: e.g. withself.view.alpha = 0.5
you get a fading effect. They have no effect on anything that isn't between them.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:In the example above,
childView
is what in your example is calledself.view
.Please write out the name of the transition; no one knows what 110 is by looking at it. It's bad style. </pedantry>