1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| class CustomTransitioningAnimator: UIPercentDrivenInteractiveTransition { var isDismiss: Bool! weak var presentViewController: UIViewController! var transitionContext: UIViewControllerContextTransitioning! // 是否处于交互视图切换过程 var interacting = false // 是否手势完成 var shouleComplete = true init(presentViewController: UIViewController) { super.init() self.presentViewController = presentViewController } }
// MARK: - 手势操作 extension CustomTransitioningAnimator { /** 处理手势操作 - parameter gestureRecognizer: */ func handleGesture(gestureRecognizer: UIPanGestureRecognizer) { let translation = gestureRecognizer.translationInView(gestureRecognizer.view) switch gestureRecognizer.state { case UIGestureRecognizerState.Began: interacting = true if presentViewController is GoodsDetailViewController { (presentViewController as! GoodsDetailViewController).delegate?.dismissPresentViewController() } case .Changed: var fraction = translation.y / 200.0 fraction = fmin(fmax(fraction, 0.0), 1.0) shouleComplete = fraction > 0.5 self.updateInteractiveTransition(fraction) case .Ended, .Cancelled: interacting = false if !shouleComplete || gestureRecognizer.state == .Cancelled || gestureRecognizer.velocityInView(gestureRecognizer.view).y < 0 { self.cancelInteractiveTransition() } else { self.finishInteractiveTransition() } default: break } } /** 开始手势拖拽 - parameter transitionContext: */ override func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning) { let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! self.transitionContext = transitionContext UIView.animateWithDuration(transitionDuration(transitionContext)) { var transform = CATransform3DIdentity transform = CATransform3DTranslate(transform, 0, 15, 0) transform.m24 = -1/5000 transform = CATransform3DScale(transform, 0.85, 1 , 1) toViewController.view.layer.transform = transform } } /** 手势拖动过程中不断更新 - parameter percentComplete: 更新百分比0~1 */ override func updateInteractiveTransition(percentComplete: CGFloat) { guard let _ = transitionContext else { return } let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! var transform = CATransform3DIdentity transform.m24 = -1/3500 + 1/3500 * percentComplete transform = CATransform3DScale(transform, 0.85 + 0.15 * percentComplete, 0.9 + 0.1 * percentComplete , 1) toViewController.view.layer.transform = transform toViewController.view.alpha = 0.5 + 0.5 * percentComplete } /** 完成手势dismiss */ override func finishInteractiveTransition() { let finalFrame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height) let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! UIView.animateWithDuration(2 * transitionDuration(transitionContext), animations: { fromViewController.view.frame = CGRect(x: 0, y: UIScreen.mainScreen().bounds.height, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height) toViewController.view.layer.transform = CATransform3DIdentity toViewController.view.alpha = 1 toViewController.view.frame = finalFrame }) { (finished) in if finished { self.transitionContext.completeTransition(true) self.transitionContext = nil } } } /** 手势取消操作 */ override func cancelInteractiveTransition() { let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)! let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)! UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in fromViewController.view.userInteractionEnabled = false fromViewController.view.frame = CGRect(x: 0, y: 0, width: fromViewController.view.frame.width, height: fromViewController.view.frame.height) }) { (finished) -> Void in if finished { UIView.animateWithDuration(self.transitionDuration(self.transitionContext), delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { var transform = CATransform3DIdentity transform = CATransform3DTranslate(transform, 0, -15, 0) transform = CATransform3DScale(transform, 0.8,0.9, 1) toViewController.view.layer.transform = transform toViewController.view.alpha = 0.5 }) { (finished) in if finished { fromViewController.view.userInteractionEnabled = true self.transitionContext.completeTransition(false) } } } } } }
|