我分類爲UINavigationController
,並創建了一個處理推送和彈出轉換的非交互式動畫對象。要完成此操作,我如何添加來處理下面的自定義流行動畫?我有點困惑從哪裏去,所以任何幫助將是偉大的,謝謝!如何添加屏幕邊緣平移手勢識別器來處理此流行動畫?
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
isNavigationBarHidden = true
}
}
extension CustomNavigationController: UINavigationControllerDelegate {
// noninteractive animator objects
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
switch operation {
case .push:
return CustomPushAnimator()
case .pop:
return CustomPopAnimator()
default:
return nil
}
}
// the interactive animator controller
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
switch animationController {
case is CustomPopAnimator:
return CustomInteractiveController()
default:
return nil
}
}
// the noninteractive push animator
class CustomPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {
// push transition duration
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.2
}
// push transition animation
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let viewWidth = UIScreen.main.bounds.width
let viewHeight = UIScreen.main.bounds.height
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: .to)
containerView.addSubview((toViewController?.view)!)
toViewController?.view.frame = CGRect(x: viewWidth, y: 0, width: viewWidth, height: viewHeight)
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
toViewController?.view.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
// the noninteractive pop animator
class CustomPopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
// pop transition duration
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.2
}
// pop transition animation
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let viewWidth = UIScreen.main.bounds.width
let viewHeight = UIScreen.main.bounds.height
let containerView = transitionContext.containerView
let fromViewController = transitionContext.viewController(forKey: .from)
let toViewController = transitionContext.viewController(forKey: .to)
containerView.insertSubview((toViewController?.view)!, belowSubview: (fromViewController?.view)!)
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
fromViewController?.view.frame = CGRect(x: viewWidth, y: 0, width: viewWidth, height: viewHeight)
}, completion: { finished in
fromViewController?.view.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
class CustomInteractiveController: NSObject, UIViewControllerInteractiveTransitioning {
func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
// I think the edge screen pan gesture goes here? Again, any advice is greatly appreciated!
}
}
對於那個低估了這個問題的人,我很感激原因。我花了很多時間研究和編寫這段代碼,並且提出了一個非常具體的問題,它給了什麼? – sconewolf