Skip to content

Instantly share code, notes, and snippets.

@JeremyXue77
Last active April 14, 2019 00:25
Show Gist options
  • Select an option

  • Save JeremyXue77/68882c0bae57484a22b6675f5a93a20f to your computer and use it in GitHub Desktop.

Select an option

Save JeremyXue77/68882c0bae57484a22b6675f5a93a20f to your computer and use it in GitHub Desktop.
protocol GestureCollectionViewDelegate: class {
func move(point: CGPoint)
func selectedItem(indexPath: IndexPath)
func cancel()
}
class GestureCollectionView: UICollectionView {
weak var gestureDelegate: GestureCollectionViewDelegate?
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let point = touches.first?.location(in: self) {
if let indexPath = self.indexPathForItem(at: point) {
gestureDelegate?.selectedItem(indexPath: indexPath)
} else {
gestureDelegate?.move(point: point)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
gestureDelegate?.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment