简体   繁体   English

UIView动画第一次不起作用

[英]UIView animation doesn't work first time

I have a seachButton in the navigation bar which upon hitting calls following method: 我在导航栏中有一个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];
}

It should load SearchViewController.xib which contains a view with UISearchBar and two buttons. 它应该加载SearchViewController.xib,其中包含带有UISearchBar和两个按钮的视图。 When I call the search method for the first time, the view appears very quickly with some weird animation, when I call it again, the animation is alright. 当我第一次调用搜索方法时,带有一些奇怪动画的视图会很快出现,而当我再次调用它时,动画就可以了。 Does anyone have a clue what could be wrong? 有人知道什么地方可能出问题吗?

Add both views to window in appdelegate, I had the same problem you had and this worked. 将两个视图添加到appdelegate中的窗口中,我遇到了与您相同的问题,并且可以正常工作。 Strange because later on I remove them from the superview but it is still working. 奇怪,因为稍后我将其从超级视图中删除,但它仍在工作。

A UIViewController does not load its view until the view method is called. 在调用view方法之前,UIViewController不会加载其视图。 I guess the problem is the view is not loaded when you call the animation first time. 我猜问题是第一次调用动画时视图未加载。 You can try to modify you code like this: 您可以尝试修改您的代码,如下所示:

if (nil == searchViewController) {
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
    searchViewController.view;
}

Try putting the animation code in the viewDidLoad function. 尝试将动画代码放入viewDidLoad函数中。 This ensures all assets and views from the nib file have been loaded and are able to be used by your app. 这样可以确保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];
}

Try putting the code for animations and view loading in the viewDidAppear: method instead of viewDidLoad: (which Chris suggested above). 尝试将用于动画和视图加载的代码放在viewDidAppear:方法中,而不是viewDidLoad: (克里斯在上面建议)。

In general, I have had problems doing view related things in viewDidLoad: . 通常,在viewDidLoad:执行视图相关的操作时遇到问题。 But doing those in viewDidAppear: has always helped (I might be missing some subtlety that causes this behavior). 但是在viewDidAppear:执行这些操作总是有帮助的(我可能会遗漏一些导致此行为的细微差别)。

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

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