SwipeCollectionViewCell.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // SwipeCollectionViewCell.swift
  3. // SwipeCellKit
  4. //
  5. // Created by Jeremy Koch
  6. // Copyright © 2017 Jeremy Koch. All rights reserved.
  7. //
  8. import UIKit
  9. /**
  10. The `SwipeCollectionViewCell` class extends `UICollectionViewCell` and provides more flexible options for cell swiping behavior.
  11. The default behavior closely matches the stock Mail.app. If you want to customize the transition style (ie. how the action buttons are exposed), or the expansion style (the behavior when the row is swiped passes a defined threshold), you can return the appropriately configured `SwipeOptions` via the `SwipeCollectionViewCellDelegate` delegate.
  12. */
  13. open class SwipeCollectionViewCell: UICollectionViewCell {
  14. /// The object that acts as the delegate of the `SwipeCollectionViewCell`.
  15. public weak var delegate: SwipeCollectionViewCellDelegate?
  16. var state = SwipeState.center
  17. var actionsView: SwipeActionsView?
  18. var scrollView: UIScrollView? {
  19. return collectionView
  20. }
  21. var indexPath: IndexPath? {
  22. return collectionView?.indexPath(for: self)
  23. }
  24. var panGestureRecognizer: UIGestureRecognizer
  25. {
  26. return swipeController.panGestureRecognizer;
  27. }
  28. var swipeController: SwipeController!
  29. var isPreviouslySelected = false
  30. weak var collectionView: UICollectionView?
  31. /// :nodoc:
  32. open override var frame: CGRect {
  33. set { super.frame = state.isActive ? CGRect(origin: CGPoint(x: frame.minX, y: newValue.minY), size: newValue.size) : newValue }
  34. get { return super.frame }
  35. }
  36. /// :nodoc:
  37. open override var isHighlighted: Bool {
  38. set {
  39. guard state == .center else { return }
  40. super.isHighlighted = newValue
  41. }
  42. get { return super.isHighlighted }
  43. }
  44. /// :nodoc:
  45. open override var layoutMargins: UIEdgeInsets {
  46. get {
  47. return frame.origin.x != 0 ? swipeController.originalLayoutMargins : super.layoutMargins
  48. }
  49. set {
  50. super.layoutMargins = newValue
  51. }
  52. }
  53. /// :nodoc:
  54. override public init(frame: CGRect) {
  55. super.init(frame: frame)
  56. configure()
  57. }
  58. /// :nodoc:
  59. required public init?(coder aDecoder: NSCoder) {
  60. super.init(coder: aDecoder)
  61. configure()
  62. }
  63. deinit {
  64. collectionView?.panGestureRecognizer.removeTarget(self, action: nil)
  65. }
  66. func configure() {
  67. contentView.clipsToBounds = false
  68. swipeController = SwipeController(swipeable: self, actionsContainerView: contentView)
  69. swipeController.delegate = self
  70. }
  71. /// :nodoc:
  72. override open func prepareForReuse() {
  73. super.prepareForReuse()
  74. reset()
  75. resetSelectedState()
  76. }
  77. /// :nodoc:
  78. override open func didMoveToSuperview() {
  79. super.didMoveToSuperview()
  80. var view: UIView = self
  81. while let superview = view.superview {
  82. view = superview
  83. if let collectionView = view as? UICollectionView {
  84. self.collectionView = collectionView
  85. swipeController.scrollView = scrollView
  86. collectionView.panGestureRecognizer.removeTarget(self, action: nil)
  87. collectionView.panGestureRecognizer.addTarget(self, action: #selector(handleCollectionPan(gesture:)))
  88. return
  89. }
  90. }
  91. }
  92. /// :nodoc:
  93. open override func willMove(toWindow newWindow: UIWindow?) {
  94. super.willMove(toWindow: newWindow)
  95. if newWindow == nil {
  96. reset()
  97. }
  98. }
  99. // Override so we can accept touches anywhere within the cell's original frame.
  100. // This is required to detect touches on the `SwipeActionsView` sitting alongside the
  101. // `SwipeCollectionViewCell`.
  102. /// :nodoc:
  103. override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  104. guard let superview = superview else { return false }
  105. let point = convert(point, to: superview)
  106. if !UIAccessibility.isVoiceOverRunning {
  107. for cell in collectionView?.swipeCells ?? [] {
  108. if (cell.state == .left || cell.state == .right) && !cell.contains(point: point) {
  109. collectionView?.hideSwipeCell()
  110. return false
  111. }
  112. }
  113. }
  114. return contains(point: point)
  115. }
  116. func contains(point: CGPoint) -> Bool {
  117. return frame.contains(point)
  118. }
  119. // Override hitTest(_:with:) here so that we can make sure our `actionsView` gets the touch event
  120. // if it's supposed to, since otherwise, our `contentView` will swallow it and pass it up to
  121. // the collection view.
  122. /// :nodoc:
  123. open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  124. guard let actionsView = actionsView else { return super.hitTest(point, with: event) }
  125. let modifiedPoint = actionsView.convert(point, from: self)
  126. return actionsView.hitTest(modifiedPoint, with: event) ?? super.hitTest(point, with: event)
  127. }
  128. /// :nodoc:
  129. override open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  130. return swipeController.gestureRecognizerShouldBegin(gestureRecognizer)
  131. }
  132. /// :nodoc:
  133. open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  134. super.traitCollectionDidChange(previousTraitCollection)
  135. swipeController.traitCollectionDidChange(from: previousTraitCollection, to: self.traitCollection)
  136. }
  137. @objc func handleCollectionPan(gesture: UIPanGestureRecognizer) {
  138. if gesture.state == .began {
  139. hideSwipe(animated: true)
  140. }
  141. }
  142. func reset() {
  143. contentView.clipsToBounds = false
  144. swipeController.reset()
  145. collectionView?.setGestureEnabled(true)
  146. }
  147. func resetSelectedState() {
  148. if isPreviouslySelected {
  149. if let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self) {
  150. collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
  151. }
  152. }
  153. isPreviouslySelected = false
  154. }
  155. }
  156. extension SwipeCollectionViewCell: SwipeControllerDelegate {
  157. func swipeController(_ controller: SwipeController, canBeginEditingSwipeableFor orientation: SwipeActionsOrientation) -> Bool {
  158. return true
  159. }
  160. func swipeController(_ controller: SwipeController, editActionsForSwipeableFor orientation: SwipeActionsOrientation) -> [SwipeAction]? {
  161. guard let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self) else { return nil }
  162. return delegate?.collectionView(collectionView, editActionsForItemAt: indexPath, for: orientation)
  163. }
  164. func swipeController(_ controller: SwipeController, editActionsOptionsForSwipeableFor orientation: SwipeActionsOrientation) -> SwipeOptions {
  165. guard let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self) else { return SwipeOptions() }
  166. return delegate?.collectionView(collectionView, editActionsOptionsForItemAt: indexPath, for: orientation) ?? SwipeOptions()
  167. }
  168. func swipeController(_ controller: SwipeController, visibleRectFor scrollView: UIScrollView) -> CGRect? {
  169. guard let collectionView = collectionView else { return nil }
  170. return delegate?.visibleRect(for: collectionView)
  171. }
  172. func swipeController(_ controller: SwipeController, willBeginEditingSwipeableFor orientation: SwipeActionsOrientation) {
  173. guard let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self) else { return }
  174. // Remove highlight and deselect any selected cells
  175. super.isHighlighted = false
  176. isPreviouslySelected = isSelected
  177. collectionView.deselectItem(at: indexPath, animated: false)
  178. delegate?.collectionView(collectionView, willBeginEditingItemAt: indexPath, for: orientation)
  179. }
  180. func swipeController(_ controller: SwipeController, didEndEditingSwipeableFor orientation: SwipeActionsOrientation) {
  181. guard let collectionView = collectionView, let indexPath = collectionView.indexPath(for: self), let actionsView = self.actionsView else { return }
  182. resetSelectedState()
  183. delegate?.collectionView(collectionView, didEndEditingItemAt: indexPath, for: actionsView.orientation)
  184. }
  185. func swipeController(_ controller: SwipeController, didDeleteSwipeableAt indexPath: IndexPath) {
  186. collectionView?.deleteItems(at: [indexPath])
  187. }
  188. }