func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{
return self
}
//MARK: - -手势只有在UIScrollView上识别
funcgestureRecognizer(_gestureRecognizer:UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:UIGestureRecognizer) ->Bool{
if let onView = otherGestureRecognizer.view, onView.isKind(of:UIScrollView.self){
return true
}
return false
}
//MARK: - -动画时长
functransitionDuration(using transitionContext:UIViewControllerContextTransitioning?) ->TimeInterval{
return1.0
}
//MARK: - -核心动画
funcanimateTransition(using transitionContext:UIViewControllerContextTransitioning) {
// 从这里
letfromVC = transitionContext.viewController(forKey: .from)as!ZHViewController
//到这里
lettoVC = transitionContext.viewController(forKey: .to)as!HomePageVC
//从这个vc
letfromView =headImageView
//临时容器 这里有个重要的概念containerView,要做转场动画的视图就必须要加入containerView上才能进行,可以理解containerView管理着所有做转场动画的视图
letcontainerView = transitionContext.containerView
var cell :HomePageCell?
ifletindex =selectIndexPath{
cell = toVC.myTableView.cellForRow(at: index)as?HomePageCell
}
lettoView = cell?.header
//截屏
letsnapShotView = fromView?.snapshotView(afterScreenUpdates:false)
snapShotView?.layer.masksToBounds=true
snapShotView?.layer.cornerRadius=15
snapShotView?.frame = containerView.convert(fromView!.frame, from: fromView?.superview)
// 加载到临时容器 让跳转动画有痕迹
containerView.insertSubview(toVC.view, belowSubview: fromVC.view)
containerView.addSubview(snapShotView!)
//title
snapShotView?.addSubview(titleLabel!)
snapShotView?.addSubview(titleTwoLabel!)
toView?.isHidden=true
fromView?.isHidden=true
UIView.animate(withDuration:self.transitionDuration(using: transitionContext), delay:0, usingSpringWithDamping:0.5, initialSpringVelocity:0.8, options: .curveEaseInOut, animations: {
containerView.layoutIfNeeded()
self.myScrollView.layoutIfNeeded()
fromVC.view.alpha=0.0
snapShotView?.layer.cornerRadius=15
self.myScrollView.frame=CGRect(x: self.myScrollView.frame.origin.x, y: self.myScrollView.frame.origin.y, width: self.myScrollView.frame.size.width, height:SCREEN_WIDTH*1.5*0.8)
self.myScrollView.layer.cornerRadius=15
snapShotView?.frame= containerView.convert(toView!.frame, from: toView?.superview)//临时的
}) { (finish)in
fromView?.isHidden=true
snapShotView?.removeFromSuperview()
toView?.isHidden=false
//动画结束告诉上下文
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}