GYSidePercentInteractiveTransition.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // GYSidePercentInteractiveTransition.swift
  3. // GYSide
  4. //
  5. // Created by gaoyuan on 2018/1/29.
  6. // Copyright © 2018年 gaoyuan. All rights reserved.
  7. //
  8. import UIKit
  9. typealias completeShowGestureBlock = (GYSideDirection) -> ()
  10. class GYSidePercentInteractiveTransition: UIPercentDrivenInteractiveTransition {
  11. var completeShowGesture:completeShowGestureBlock?
  12. var isInteractive:Bool! = false
  13. weak var _targetVC:UIViewController!
  14. var _config:GYSideConfig!
  15. var _showType:GYSideShowType!
  16. private var _direction:GYSideDirection?
  17. private var _percent:CGFloat = 0.0 //必须用全局的
  18. init(showType:GYSideShowType,viewController:UIViewController?,config:GYSideConfig?) {
  19. super.init()
  20. _showType = showType
  21. _targetVC = viewController
  22. _config = config
  23. NotificationCenter.default.addObserver(self, selector: #selector(gy_tapAction), name: NSNotification.Name(rawValue:GYSideTapNotification), object: nil)
  24. NotificationCenter.default.addObserver(self, selector: #selector(gy_panAction(_ :)), name: NSNotification.Name(rawValue:GYSidePanNotification), object: nil)
  25. }
  26. @objc func gy_tapAction() {
  27. if _showType == .show {return}
  28. _targetVC?.dismiss(animated: true, completion: nil)
  29. self.finish()
  30. }
  31. @objc func gy_panAction(_ sender:Notification) {
  32. let pan:UIPanGestureRecognizer = sender.object as! UIPanGestureRecognizer
  33. if _showType == .hidden {
  34. handlePan(pan: pan)
  35. }
  36. }
  37. func addPanGesture(fromViewController:UIViewController) {
  38. let pan:UIPanGestureRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(handlePan(pan:)))
  39. fromViewController.view.addGestureRecognizer(pan)
  40. }
  41. //手势blcok 回调
  42. func handlePresentPan(pan:UIPanGestureRecognizer) {
  43. var x:CGFloat = pan.translation(in: pan.view).x // -左划 +右滑
  44. let width:CGFloat = (pan.view!.bounds.width)
  45. var percent:CGFloat = 0.0
  46. switch pan.state {
  47. case .began:
  48. if x<0 {
  49. _direction = .right;
  50. }else if x>=0 {
  51. _direction = .left;
  52. }
  53. isInteractive = true
  54. if (completeShowGesture != nil) {
  55. completeShowGesture!(_direction!)
  56. }
  57. break
  58. case .changed:
  59. if _direction == GYSideDirection.right {
  60. x = x>0.0 ? 0.0:x;
  61. }else {
  62. x = x<0.0 ? 0.0:x;
  63. }
  64. percent = CGFloat(fabsf(Float(x/width)))
  65. percent = percent<=0.0 ? 0.0:percent
  66. percent = percent>=1.0 ? 1.0:percent
  67. _percent = percent
  68. self.update(percent)
  69. break
  70. case .ended:
  71. isInteractive = false
  72. if _percent < 0.5 {
  73. self.cancel()
  74. }else {
  75. self.finish()
  76. }
  77. break
  78. case .cancelled:
  79. isInteractive = false
  80. self.cancel()
  81. break
  82. default:
  83. break
  84. }
  85. }
  86. @objc func handlePan(pan: UIPanGestureRecognizer) {
  87. var x:CGFloat = pan.translation(in: pan.view).x // -左划 +右滑
  88. if _config == nil && _showType == .show{
  89. self.handlePresentPan(pan: pan)
  90. return
  91. }
  92. var width:CGFloat = (pan.view!.bounds.width) // 手势驱动时 相对移动的宽度
  93. if _config.animationType == .zoom {
  94. width = kScreenWidth*(1.0 - _config.zoomOffsetRelative)
  95. }
  96. var percent:CGFloat = 0.0
  97. switch pan.state {
  98. case .began :
  99. isInteractive = true;
  100. _targetVC.dismiss(animated: true, completion: nil)
  101. break;
  102. case .changed:
  103. if _config.direction == GYSideDirection.left && _showType == .hidden {
  104. x = x>0.0 ? 0.0:x;
  105. }else {
  106. x = x<0.0 ? 0.0:x;
  107. }
  108. percent = CGFloat(fabsf(Float(x/width)))
  109. percent = percent<=0.0 ? 0.0:percent
  110. percent = percent>=1.0 ? 1.0:percent
  111. _percent = percent
  112. self.update(percent)
  113. break
  114. case .ended:
  115. isInteractive = false
  116. if _percent < 0.5 {
  117. self.cancel()
  118. }else {
  119. self.finish()
  120. }
  121. break
  122. case .cancelled:
  123. isInteractive = false
  124. self.cancel()
  125. break
  126. default:
  127. break
  128. }
  129. }
  130. deinit {
  131. NotificationCenter.default.removeObserver(self)
  132. // print( NSStringFromClass(self.classForCoder) + " 销毁了---->5")
  133. }
  134. }