我想要做的是一个自定义的动画,把ViewController从左侧的端推出来。我已经创建了我的自定义转换委托,我提供了我的自定义动画,一切正常工作(从左边的新视图幻灯片)。唯一的问题是,iOS中的push动画不仅仅是从右侧滑动视图。被模糊的VC也在向与被推送的VC相同的方向移动。还有导航条有点闪烁。当然,我可以尝试通过猜测参数应该是什么来模仿这种行为(例如,在不同的iPhones上被模糊化的VC移动了多少),但是也许可以在某个地方找到值呢?非常感激的帮助。
发布于 2016-05-27 23:46:05
我将创建一个符合UIViewControllerAnimatedTransitioning协议的对象。
class CustomHorizontalSlideTransition: NSObject, UIViewControllerAnimatedTransitioning {
var operation: UINavigationControllerOperation = .Push
convenience init(operation: UINavigationControllerOperation) {
self.init()
self.operation = operation
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let disappearingVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let appearingVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let bounds = UIScreen.mainScreen().bounds
if self.operation == .Push {
appearingVC.view.frame = CGRectOffset(bounds, -bounds.size.height, 0)
containerView!.addSubview(disappearingVC.view)
containerView!.addSubview(appearingVC.view)
} else {
appearingVC.view.frame = bounds
disappearingVC.view.frame = bounds
containerView!.addSubview(appearingVC.view)
containerView!.addSubview(disappearingVC.view)
}
UIView.animateWithDuration(transitionDuration(transitionContext),
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: { () -> Void in
if self.operation == .Push {
appearingVC.view.frame = bounds
} else {
disappearingVC.view.frame = CGRectOffset(bounds, -bounds.size.width, 0)
}
}) { (complete) -> Void in
transitionContext.completeTransition(true)
}
}
}然后,在“从”和“到”视图控制器中,在视图ViewDidAppear中将导航控制器的委托设置为self
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.delegate = self
}在两个视图控制器中,重写以下内容以提供transitionAnimatedTransition委托方法,并返回动画中遵循协议的实例
override func transitionAnimatedTransition(operation: UINavigationControllerOperation) -> UIViewControllerAnimatedTransitioning? {
return CustomHorizontalSlideTransition(operation: operation)
}https://stackoverflow.com/questions/37493624
复制相似问题