使用模态动画(水平翻转)推送ViewController

5
我需要将一个视图控制器推到另一个视图控制器上。
menuVC -> VC1 ->VC2

从菜单VC到VC1不需要动画,但是从VC1到VC2和从VC2到VC1需要进行翻转动画。

然而,当从VC2到菜单VC时,不需要动画。

我正在使用以下代码:

(来自如何使用翻转动画推送视图控制器)

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
FlashCardVC *flashCardVC = [[FlashCardVC alloc] init];
[self.navigationController pushViewController:flashCardVC animated:NO];
[UIView commitAnimations];

我尝试进行上述操作时,屏幕完全变黑。
有什么问题吗?
3个回答

18

现在您应该使用基于块的动画。此外,如果您正在从同一故事板中的控制器实例化故事板控制器,您可以更轻松地使用self.storyboard访问该故事板。

-(IBAction)flipToNext:(id)sender {
    SecondController *next = [self.storyboard instantiateViewControllerWithIdentifier:@"Next"];
    [self.navigationController pushViewController:next animated:NO];
    [UIView transitionWithView:self.navigationController.view duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil];
}

有没有关于如何更新这个答案以适应iOS 8风格转换的想法? - SleepsOnNewspapers
@hsavit1,你在问什么关于iOS 8风格的过渡效果? - rdelmar

3

在Swift 4.2中使用这些扩展

用于推入和弹出视图控制器时使用水平翻转动画

extension UINavigationController {

    func pushViewControllerWithFlipAnimation(viewController:UIViewController){
        self.pushViewController(viewController
        , animated: false)
        if let transitionView = self.view{
            UIView.transition(with:transitionView, duration: 0.8, options: .transitionFlipFromLeft, animations: nil, completion: nil)
        }
    }

    func popViewControllerWithFlipAnimation(){
        self.popViewController(animated: false)
        if let transitionView = self.view{
            UIView.transition(with:transitionView, duration: 0.8, options: .transitionFlipFromRight, animations: nil, completion: nil)
        }
    }
}

1
优秀的回答: 显示pushviewcontroller动画看起来像presentModalViewController (代码)
    //UIViewController *yourViewController = [[UIViewController alloc]init];</s>

    [UIView  beginAnimations: @"Showinfo"context: nil];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [self.navigationController pushViewController: yourViewController animated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];

然而,这也会产生一个空白屏幕。

相反,如果您正在使用故事板,则最好通过故事板进行实例化:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle bundleForClass:[self class]]];

YourViewController *yourViewController = [storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];

yourViewControllerID 是在故事板中为 yourviewcontroller 类在身份检查器下设置的。


请标记此答案为已接受,以便此问题不会出现在未回答的问题列表中。 - memmons

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接