1
我有一个导航控制器,并在该导航控制器的内部,我有一个主屏幕,从主屏幕我点击一个按钮,转到另一个屏幕。自定义转换
但使用导航控制器时的标准显示动画是从侧面滑动,但我想要做的是视图控制器从屏幕底部向上滑动,并在到达时创建一种弹跳动画顶端。
我有一个导航控制器,并在该导航控制器的内部,我有一个主屏幕,从主屏幕我点击一个按钮,转到另一个屏幕。自定义转换
但使用导航控制器时的标准显示动画是从侧面滑动,但我想要做的是视图控制器从屏幕底部向上滑动,并在到达时创建一种弹跳动画顶端。
任何想使用自定义转换的人都需要记住UIViewControllerAnimatedTransitioning
和UIViewControllerTransitioningDelegate
协议。现在,顺应UIViewControllerAnimatedTransitioning
您customclass内从NSObject
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
})
}
}
上述方法继承将动画的照顾。之后,创建 上述目的和使用yourViewController类中
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)
}
注:为了看到动画。在呈现ViewController时,千万不要将动画标志设置为假 。否则,你的动画将不会工作, 。
最后实现UIViewControllerTransitioningDelegate
协议方法里面YourViewcontroller
类
extension YourViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return customPushAnimation
}
每当您提出上述协议的方法 打来电话,你的动画魔力就会出现视图 - 控制。
你到目前为止尝试过什么?显示您尝试使用的代码,并解释您遇到问题的位置。如果你还没有尝试过任何东西,跳到谷歌(或你最喜欢的搜索引擎)并搜索'uinavigationcontroller自定义转换... ...你会发现很多的例子,讨论,教程等等,等等。 – DonMag