Ich unterordnete UINavigationController
und erstellte ein nicht interaktives Animationsobjekt, das die Push- und Pop-Übergänge behandelt. Wie füge ich einen UIScreenEdgePanGestureRecognizer
hinzu, um die unten stehende benutzerdefinierte Pop-Animation zu bearbeiten? Ich bin irgendwie verwirrt darüber, wohin ich von hier gehen sollte, also wäre jede Hilfe großartig, danke!Wie füge ich einen Pan-Gesten-Erkenner für den Bildschirmrand hinzu, um mit dieser Pop-Animation umzugehen?
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!
}
}
Für die Person, die diese Frage abgelehnt hat, würde ich einen Grund zu schätzen wissen. Ich habe viel Zeit damit verbracht, diesen Code zu recherchieren und zu schreiben, und ich habe eine sehr spezifische Frage gestellt, was gibt es? – sconewolf