我为解雇我的详细视图控制器做了一个自定义UIViewControllerAnimatedTransitioning
,如下所示:
class DismissAnimator : NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.4
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
guard let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)?.childViewControllers.first as? MainController,
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? DetailViewController
else {
return
}
containerView.insertSubview(toVC.view, belowSubview: fromVC.view)
fromVC.view.isHidden = true
let snapshot = fromVC.view.snapshotView(afterScreenUpdates: false)
containerView.insertSubview(snapshot!, aboveSubview: toVC.view)
UIView.animate(
withDuration: transitionDuration(using: transitionContext),
animations: {
snapshot!.center.y += UIScreen.main.bounds.height
},
completion: { _ in
fromVC.view.isHidden = false
snapshot?.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
我的MainViewController
具有以下功能:
extension MainController: UIViewControllerTransitioningDelegate {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openDetailView" {
let cell = sender as! PopularCell
let indexPath = popularCollectionView.indexPath(for: cell)
let destinationViewController = segue.destination as! DetailViewController
destinationViewController.transitioningDelegate = self
destinationViewController.event = events[indexPath!.row]
destinationViewController.interactor = interactor
}
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return OpeningAnimator()
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return DismissAnimator()
}
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactor.hasStarted ? interactor : nil
}
这就是我的平移互动的处理方式:
@IBAction func handleGesture(sender: UIPanGestureRecognizer) {
let percentThreshold:CGFloat = 0.3
let translation = sender.translation(in: view)
let progress = progressAlongAxis(pointOnAxis: translation.y, axisLength: view.bounds.height)
guard let interactor = interactor,
let originView = sender.view else { return }
switch originView {
case view:
break
case tableView:
if tableView.contentOffset.y > 0 {
return
}
default:
break
}
switch sender.state {
case .began:
interactor.hasStarted = true
dismiss(animated: true, completion: nil)
case .changed:
interactor.shouldFinish = progress > percentThreshold
interactor.update(progress)
case .cancelled:
interactor.hasStarted = false
interactor.cancel()
case .ended:
interactor.hasStarted = false
interactor.shouldFinish
? interactor.finish()
: interactor.cancel()
default:
break
}
}
不幸的是,只要我几乎没有拖动模态视图控制器,它就会自动消失,没有交互性。在handleGesture(sender:)
中设置一个断点表明该函数正在被调用,所以我对我现在应该做什么感到困惑。
任何帮助?