指定 "show"(推)segue 的方向
Specify direction of "show" (push) segue
是否有一种简单的方法来指定 "show" 转场(以前称为 "push" 转场)的方向(从左、右、上或下)?
默认 "show" 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
的方式已过时。您应该使用 transitionContext.viewForKey(UITransitionContextToViewKey)
而不是 toViewController.view
)
一个例子:
首先,您需要设置故事板,我假设您已经完成了。
其次,你需要一个符合UIViewControllerAnimatedTransitioning
的NSObject
子class。这将用于控制从一个视图控制器到下一个视图控制器的转换。
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
的委托。以下 NavigationControllerDelegate
class 将用于 return 在进行推送转场时 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
}
}
应该就是这样!要对此进行扩展,我建议您查看交互式过渡。
是否有一种简单的方法来指定 "show" 转场(以前称为 "push" 转场)的方向(从左、右、上或下)?
默认 "show" 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
的方式已过时。您应该使用 transitionContext.viewForKey(UITransitionContextToViewKey)
而不是 toViewController.view
)
一个例子:
首先,您需要设置故事板,我假设您已经完成了。
其次,你需要一个符合UIViewControllerAnimatedTransitioning
的NSObject
子class。这将用于控制从一个视图控制器到下一个视图控制器的转换。
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
的委托。以下 NavigationControllerDelegate
class 将用于 return 在进行推送转场时 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
}
}
应该就是这样!要对此进行扩展,我建议您查看交互式过渡。