[英]UIView animation doesn't work first time
我在导航栏中有一个seachButton,当点击以下方法时会调用它:
- (IBAction)search:(id)sender
{
if (nil == searchViewController)
searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
searchViewController.view.backgroundColor = [UIColor clearColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:searchViewController.view
cache:NO];
[self.view addSubview:searchViewController.view];
[UIView commitAnimations];
}
它应该加载SearchViewController.xib,其中包含带有UISearchBar和两个按钮的视图。 当我第一次调用搜索方法时,带有一些奇怪动画的视图会很快出现,而当我再次调用它时,动画就可以了。 有人知道什么地方可能出问题吗?
将两个视图添加到appdelegate中的窗口中,我遇到了与您相同的问题,并且可以正常工作。 奇怪,因为稍后我将其从超级视图中删除,但它仍在工作。
在调用view方法之前,UIViewController不会加载其视图。 我猜问题是第一次调用动画时视图未加载。 您可以尝试修改您的代码,如下所示:
if (nil == searchViewController) {
searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
searchViewController.view;
}
尝试将动画代码放入viewDidLoad函数中。 这样可以确保nib文件中的所有资产和视图均已加载,并且可以被您的应用使用。
- (IBAction)search:(id)sender
{
if (nil == searchViewController)
searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
[self.view addSubview:searchViewController.view];
}
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor clearColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view
cache:NO];
[UIView commitAnimations];
}
尝试将用于动画和视图加载的代码放在viewDidAppear:
方法中,而不是viewDidLoad:
(克里斯在上面建议)。
通常,在viewDidLoad:
执行视图相关的操作时遇到问题。 但是在viewDidAppear:
执行这些操作总是有帮助的(我可能会遗漏一些导致此行为的细微差别)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.