iOS UIViewAnimation无效

时间:2012-12-13 19:55:59

标签: ios animation uiview uiviewcontroller viewdidappear

我有一个主详细信息应用程序,当详细视图出现且字符串为空时,我希望它通过UIViewAnimationFlip呈现新视图。动画正在运行,但它一直在翻转,而不是我启动的视图控制器。任何帮助都会很棒!

- (void)viewDidAppear:(BOOL)animated {

    if (masterView.parserURL == nil) {

        LoginViewController *login = [[LoginViewController alloc] init];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.8];
        [UIView setAnimationTransition:UIModalTransitionStyleFlipHorizontal
                               forView:self.view
                                 cache:YES];

        [self.navigationController presentViewController:login 
                                                animated:YES 
                                              completion:nil];

        [UIView commitAnimations];
    }
}

2 个答案:

答案 0 :(得分:1)

我同意您应该以模态方式执行此操作,而不是仅添加子视图。在您的示例代码中,您将动画两次,因为presentviewcontroller方法已经为自己设置了动画。尝试删除其他动画代码,如下所示:

LoginViewController *login = [[LoginViewController alloc] init];
login.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentViewController:login 
                   animated:YES 
                 completion:nil];

答案 1 :(得分:0)

我建议简单地说:

- (void)viewDidAppear:(BOOL)animated {

    if (masterView.parserURL == nil) {

        LoginViewController *login = [[LoginViewController alloc] init];

        login.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:login
                           animated:YES
                         completion:nil];
    }
}

注意,有一个有趣的问题,即登录屏幕应如何更新parserURL中的masterView字段。您可以向登录控制器添加一个指向masterView的指针的属性,以便它具有更新parserURL的机制。因此可能就像:

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

    if (masterView.parserURL == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];

        login.masterView = masterView;

        login.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:login
                           animated:YES
                         completion:nil];
    }
}

然后您的登录控制器现在可以通过以下方式更新parserURL:

self.masterView.parserURL = ... // set it as appropriate