iOS 7 改变 pushviewcontroller 动画方向

标签 ios animation push direction

当我按下 View Controller 时,动画总是从右到左。我想将该动画更改为从左到右反转。 我试过这段代码,但没有用。动画还是从右到左。我怎样才能简单而正确地实现这一点?请需要帮助。提前致谢。

//NOT WORKING
myViewController *mController = [self.storyboard instantiateViewControllerWithIdentifier:@"myViewController"];

CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;

[self.view.layer addAnimation:transition forKey:nil];

[self.navigationController pushViewController:mController animated:YES];

最佳答案

由于这个问题提到了 iOS 7,我很惊讶答案没有提到 iOS 7 中引入的 Animated Transitions API。

如果你想直接进入 GitHub,objc.io 的人有一篇很棒的文章,其中有一个链接项目 here

查看文档,您将看到以下内容:

@availability(iOS, introduced=7.0)
optional func navigationController(navigationController: UINavigationController, 
animationControllerForOperation operation: UINavigationControllerOperation, 
fromViewController fromVC: UIViewController,
 toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

或者在 Objective-C 中

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

这意味着从 iOS 7 开始,您可以控制导航 Controller 如何实现其推送和弹出动画。

您所需要做的就是设置导航 Controller 的委托(delegate),并在适当的时候提供正确的动画对象。那么让我们开始吧:

1。设置 UINavigationControllerDelegate

我通常喜欢有一个 App-Wide Navigation Coordinator 来管理我的所有转换等。因此理想情况下,您需要一些在 NavigationController 的持续时间内保持事件状态的对象。所以我们可能有一个看起来像这样的协调器:

class NavigationCoordinationController: NSObject {
    private(set) var navigationController:UINavigationController        

    required init(navigationController: UINavigationController) {
        self.navigationController = navigationController
        super.init()
        navigationController.delegate = self
    }
}

我通常在 App Delegate 中创建它,以便可以在任何地方引用它。您还可以在每个 View Controller 的基础上设置委托(delegate),创建自定义 UINavigationController 子类,或者您认为合适的任何地方。

2。创建自定义动画师

现在我们需要一个符合UIViewControllerAnimatedTransitioning 协议(protocol)的对象。只要需要动画过渡,就会调用此对象来应用动画。此示例是一个简单的动画,它在过渡中淡化和扩展 fromView,因此它与推送相关。

class CustomPushExpansionTransitioner: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 1
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let fromView: UIView = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        let toView: UIView = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let container = transitionContext.containerView()
        container.addSubview(toView)
        container.bringSubviewToFront(fromView)

        toView.frame = container.convertRect(fromView.bounds, fromView: fromView)

        UIView.animateWithDuration(transitionDuration(transitionContext),
            animations: { () -> Void in
                fromView.alpha = 0
                fromView.transform = CGAffineTransformConcat(fromView.transform, CGAffineTransformMakeScale(1.3, 1.3))
        }) { (complete) -> Void in
            transitionContext.completeTransition(true)
        }
    }
}

3。必要时出售动画师

现在我们需要实现 UINavigationController 委托(delegate)并在需要时提供我们的自定义动画器。

extension NavigationCoordinationController: UINavigationControllerDelegate {
    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        switch operation {
        case .Push:
            return LaunchToHomeTransitioner()
        case .Pop:
            break
        case .None:
            break
        }
        return nil
    }
}

好了,你现在可以完全控制你的 UINavigationController 转换

关于iOS 7 改变 pushviewcontroller 动画方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26465732/

相关文章:

css - 单击外部按钮扩展搜索栏

mercurial - 乌龟Hg 2.0 : Push Branch

mercurial - HTTP 错误 502 : Bad Gateway when pushing to bitbucket

ios - 重新编译应用程序后生成预览图像时,视频从 URL 中消失

javascript - 仅使用 CSS 的 div fadeIn 和 fadeOut(从 DOM 追加和删除)。是否可以?

ios - 我是否需要始终创建嵌套结构以从json响应中的嵌套模型中仅获取一个属性?

iphone - 我们的淡入淡出动画卡住了按钮和 slider 控件

android - adb push <directory>,但跳过 .svn .git(和 friend )

iphone - 在 iOS 中查看内存使用情况

ios - 为什么 SwiftUI 中的 Today App Extension Widget 是白色的?