【发布时间】:2015-05-30 22:17:18
【问题描述】:
有没有一种简单的方法来指定“show”segue(以前称为“push”segue)的方向(从左、右、上或下)?
默认的“show”segue 从左到右,但我希望它从下到上。
【问题讨论】:
标签: ios objective-c swift segue
有没有一种简单的方法来指定“show”segue(以前称为“push”segue)的方向(从左、右、上或下)?
默认的“show”segue 从左到右,但我希望它从下到上。
【问题讨论】:
标签: ios objective-c swift segue
我不相信有内置的方法可以实现这一点,但是您可以使用 iOS7 中引入的自定义过渡。那里有很多很棒的教程,我在下面添加了一些链接和一些示例代码,演示了如何从顶部向下滑动新的视图控制器。
要查看的网站:
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))
一个例子:
首先,您需要设置故事板,我假设您已经完成了。
其次,您需要一个符合UIViewControllerAnimatedTransitioning 的NSObject 子类。这将用于控制从一个视图控制器到下一个视图控制器的转换。
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 的委托。当执行 push segue 时,以下 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
}
}
应该就是这样!要对此进行扩展,我建议您查看 Interactive Transitions。
【讨论】: