【问题标题】:How to use animateWithDuration: when presenting view controller on iOS 7?如何使用 animateWithDuration:在 iOS 7 上呈现视图控制器时?
【发布时间】:2014-04-08 12:38:13
【问题描述】:

我目前正在使用以下代码来展示我的视图控制器。

CATransition* transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromBottom;
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:viewController animated:NO completion:nil];

我需要使用UIView animateWithDuration 方法使用更复杂的动画。它只是模态演示,背景视图控制器应该留在那里。如何动画呈现视图控制器的视图?

更新:下一个版本的代码:)

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:NULL];
RootViewControllerEx *viewController = (RootViewControllerEx *)[sb instantiateViewControllerWithIdentifier:@"SB_RDVC"];
viewController.transitioningDelegate = self;
viewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:viewController animated:YES completion:nil];
...

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    ...
}

故事板中的VC没有指定任何se​​gue,它在故事板中保持孤独:)

【问题讨论】:

    标签: ios ios7 core-animation


    【解决方案1】:

    您需要使用UIModalPresentationCustom 作为您的UIViewController's modalPresentationStyle

    然后您需要创建一个符合UIViewControllerTransitioningDelegate 的类并将其设置为presentedViewController

    例子

    YourViewController *viewController = [[YourViewController alloc] init];
    viewController.delegate = self;
    viewController.transitioningDelegate = self;
    viewController.modalPresentationStyle = UIModalPresentationCustom;
    
    [self.navigationController presentViewController:viewController animated:YES completion:nil];
    

    然后您必须实现以下方法:

    - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
    

    它返回一个符合UIViewControllerAnimatedTransitioning 协议的对象,它实现了以下方法:

    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    {
        UIView *inView = [transitionContext containerView];
        UIView *toView = [[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey] view];
    
        toView.frame = //CGRectMake(0,0,0,0);
        [inView addSubview:toView];
        // Use whatever animateWithDuration you need
        [UIView animateWithDuration:0.6f delay:0.0f usingSpringWithDamping:0.7f initialSpringVelocity:0.5f options:UIViewAnimationOptionCurveEaseIn animations:^{
    // Custom animation here
        } completion:^(BOOL finished) {
            // IMPORTANT
            [transitionContext completeTransition:YES];
        }];
    }
    

    请务必记住在转换完成时告知上下文,否则您将进入不稳定状态。

    【讨论】:

    • 和我刚开始打字的差不多,所以我放弃了,而是投了你一票
    • 不知何故,我得到了我一直拥有的相同动画 - 从底部出现。动画委托从未被调用。可能是因为我使用了instantiateViewControllerWithIdentifier,而我的 VC 是在 Storyboard 中定义的?
    • 您是否将演示文稿样式设置为 UIModalPresentationCustom?
    • 是的,我的班级被声明为实现UIViewControllerTransitioningDelegate 协议。查看更新。
    • -> UIModalPresentationCustom 演示样式只能用于动画制作者或非动画演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多