Guest User

Untitled

a guest
Jan 12th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. - (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
  2. shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
  3. {
  4. return YES;
  5. }
  6.  
  7. @IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
  8. @IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!
  9.  
  10. myPanGesture.require(toFail: mySwipeGestureRecognizer)
  11.  
  12. let minHeight: CGFloat = 100
  13. let maxHeight: CGFloat = 700
  14. let swipeVelocity: CGFloat = 500
  15. var previousTranslationY: CGFloat = 0
  16.  
  17. @IBOutlet weak var cardHeightConstraint: NSLayoutConstraint!
  18.  
  19. @IBAction func didPanOnCard(_ sender: Any) {
  20.  
  21. guard let panGesture = sender as? UIPanGestureRecognizer else { return }
  22.  
  23. let gestureEnded = bool(panGesture.state == UIGestureRecognizerState.ended)
  24. let velocity = panGesture.velocity(in: self.view)
  25.  
  26. if gestureEnded && abs(velocity.y) > swipeVelocity {
  27. handlePanOnCardAsSwipe(withVelocity: velocity.y)
  28. } else {
  29. handlePanOnCard(panGesture)
  30. }
  31. }
  32.  
  33. func handlePanOnCard(_ panGesture: UIPanGestureRecognizer) {
  34.  
  35. let translation = panGesture.translation(in: self.view)
  36. let translationYDelta = translation.y - previousTranslationY
  37.  
  38. if abs(translationYDelta) < 1 { return } // ignore small changes
  39.  
  40. let newCardHeight = cardHeightConstraint.constant - translationYDelta
  41.  
  42. if newCardHeight > minHeight && newCardHeight < maxHeight {
  43. cardHeightConstraint.constant = newCardHeight
  44. previousTranslationY = translation.y
  45. }
  46.  
  47. if panGesture.state == UIGestureRecognizerState.ended {
  48. previousTranslationY = 0
  49. }
  50. }
  51.  
  52. func handlePanOnCardAsSwipe(withVelocity velocity: CGFloat) {
  53. if velocity.y > 0 {
  54. dismissCard() // implementation not shown
  55. } else {
  56. maximizeCard() // implementation not shown
  57. }
  58. }
Add Comment
Please, Sign In to add comment