gpt4 book ai didi

ios - 自定义过渡

转载 作者:行者123 更新时间:2023-11-28 08:05:20 25 4
gpt4 key购买 nike

我有一个导航 Controller ,在该导航 Controller 内部我有一个主屏幕,从主屏幕我单击一个按钮可以转到另一个屏幕。

但是使用导航 Controller 时的标准显示动画是它从侧面滑动,但我想做的是 View Controller 从屏幕底部向上滑动并在到达时创建一种弹跳动画顶。

最佳答案

任何想使用自定义过渡的人都需要记住 UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate 协议(protocol)。现在在继承自 NSObject

的自定义类中使用 UIViewControllerAnimatedTransitioning
import UIKit
class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.2
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerVw = transitionContext.containerView
let fromViewController = transitionContext.viewController(forKey: .from)
let toViewController = transitionContext.viewController(forKey: .to)
guard let fromVc = fromViewController, let toVc = toViewController else { return }
let finalFrame = transitionContext.finalFrame(for: toVc)

//For different animation you can play around this line by changing frame
toVc.view.frame = finalFrame.offsetBy(dx: 0, dy: finalFrame.size.height/2)
containerVw.addSubview(toVc.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toVc.view.frame = finalFrame
fromVc.view.alpha = 0.5
}, completion: {(finished) in
transitionContext.completeTransition(finished)
fromVc.view.alpha = 1.0
})
}
}

The above method will take care of the animation. After that create the above object and use inside yourViewController class

import UIKit
class YourViewController: UIViewController {
lazy var customPushAnimation: CustomPushAnimation = {
return CustomPushAnimation()
}()
func openViewControler() {
}let vc =//Assuming your view controller which you want to open
let navigationController = UINavigationController(rootViewController: vc)
//Set transitioningDelegate to invoke protocol method
navigationController.transitioningDelegate = self
present(navigationController, animated: true, completion: nil)
}

Note: In order to see the animation. Never set animation flag to false while presenting the ViewController. Otherwise your animation will never work.

最后在YourViewcontroller类中实现UIViewControllerTransitioningDelegate协议(protocol)方法

extension YourViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPushAnimation
}

Whenever you present the viewcontroller above protocol method will called and your animation magic will appear.

关于ios - 自定义过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45338069/

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