是否有一种简单的方法来指定“显示”转场(以前称为“推”转场)的方向(从左、右、上或下)?
默认的“显示”转场从左到右,但我希望它从下到上。
最佳答案
我认为没有内置方法可以实现这一点,但是您可以使用 iOS7 中引入的自定义过渡。那里有很多很棒的教程,我在下面添加了一些链接和一些示例代码,演示了如何将新的 View Controller 从顶部向下滑动。
要检查的站点:
Introduction to Custom View Controller Transitions and Animations
RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App
(注意:在第三和第四教程中访问UIViewController
的view
的方式已经过时了。取而代之的是toViewController.view
你应该使用 transitionContext.viewForKey(UITransitionContextToViewKey)
)
一个例子:
首先,您需要设置 Storyboard,我假设您已经完成了。
其次,您需要一个符合UIViewControllerAnimatedTransitioning
的NSObject
子类。这将用于控制从一个 View Controller 到下一个 View Controller 的转换。
class TransitionManager : NSObject, UIViewControllerAnimatedTransitioning {
let animationDuration = 0.5
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return animationDuration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
containerView.addSubview(toView)
let finalFrame = containerView.bounds
let initalFrame = CGRect(x: 0, y: -finalFrame.height, width: finalFrame.width, height: finalFrame.height)
toView.frame = initalFrame
UIView.animateWithDuration(animationDuration,
animations: { toView.frame = finalFrame },
completion: { _ in transitionContext.completeTransition(true) })
}
}
如 RayWenderlich: How To Make A View Controller Transition Animation Like in the Ping App 中所述教程中,您需要设置 UINavigationController
的委托(delegate)。以下 NavigationControllerDelegate
类将用于在执行推送转场时返回 TransitionManager
的实例:
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
let transitionManager = TransitionManager()
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .Push {
return transitionManager
}
return nil
}
}
应该就是这样!要对此进行扩展,我建议您查看交互式过渡。
关于ios - 指定 "show"(推)segue 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30551909/