gpt4 book ai didi

ios - PushViewController 详细信息?

转载 作者:行者123 更新时间:2023-11-30 13:23:29 26 4
gpt4 key购买 nike

我想做的是从左侧插入ViewController的自定义动画。我已经创建了自定义过渡委托(delegate),并提供了自定义动画,一切正常(新 View 从左侧幻灯片)。唯一的问题是 iOS 中的推送动画不仅仅是从右侧滑动 View 。被遮挡的 VC 也在与被插入的 VC 相同的方向上轻微移动。另外,导航栏有点闪烁。我当然可以尝试通过猜测参数应该是什么来模仿这种行为(例如,被遮挡的 VC 在不同的 iPhone 上移动了多少),但也许可以在某个地方找到这些值?非常感谢您的帮助。

最佳答案

我将创建一个遵守 UIViewControllerAnimatedTransitioning 协议(protocol)的对象

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)
}
}
}

然后在“From”和“To” View Controller 中,将 navigationController 的委托(delegate)设置为 View ViewDidAppear 中的 self

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.delegate = self
}

在两个 View Controller 中,重写以下内容以提供 transitionAnimatedTransition 委托(delegate)方法并返回动画的协议(protocol)遵守实例

override func transitionAnimatedTransition(operation: UINavigationControllerOperation) -> UIViewControllerAnimatedTransitioning? {
return CustomHorizontalSlideTransition(operation: operation)
}

关于ios - PushViewController 详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com