5

I have a uiview with multipe buttons and images on it. I drag that card kind of like a tinder interface.

I want to be able stop the user dragging the card at some points and allow them at others.

I tried disabling it but the way I was doing it disabled all user interaction with the whole view stopping the swiping but also stopping users hitting the button which I don't want to do (I only want to stop them swiping).

This is how I initiate the swipe:

let gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))
backview.addGestureRecognizer(gestureBack)

This is how I incorrectly attempted to stop the swiping:

  self.backview.userInteractionEnabled = true

How can I just stop the view from swiping without it affecting all the other buttons ect inside the view.

Thanks

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

4 Answers4

6

yes for this there is a delegate method's for UIGestureRecognizer u can set the delegate method like below

let gestureBack = UIPanGestureRecognizer(target: self, action: Selector("wasDraggedBack:"))
backview.addGestureRecognizer(gestureBack)
//set the delegate
 gestureBack.delegate? = self

and also confirm to delegate like,

class ViewController: UIViewController,UIGestureRecognizerDelegate {

}

and finally use this delegate method to trigger action,

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    //condition to recognise the gesture 
    return true; //or false
}
Shankar BS
  • 8,394
  • 6
  • 41
  • 53
4

According to the docs, all UIGestureRecognizer class objects has a enabled property.

What it does is:

Disables a gesture recognizers so it does not receive touches. The default value is true. If you change this property to false while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

bevoy
  • 751
  • 4
  • 14
3

You can do something like,

 gestureBack.enabled = false
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

TL;DR: You need to disable the gesture.

gesture.isEnabled = enabled

I have several view objects with gestures registered. The solution I have implemented is as follows.

  1. Created a function that will enable/disable gestures on all views in a controller except for the selected view.
/// Set other views interaction off except the selected view
/// Prevent the image accidentally taking over
private func setViewGestures(enabled: Bool, excludedView: UIView) {
    for subview in view.subviews {
        if subview == excludedView {
            continue
        }
        // Disable gestures
        if let gestures = subview.gestureRecognizers {
            for gesture in gestures {
                gesture.isEnabled = enabled
            }
        }
    }
}
  1. Next, in the UIGestureRecognizerDelegate function:
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    guard let gestureView = gestureRecognizer.view else {
        return false
    }
    setViewGestures(enabled: false, excludedView: gestureView)
}
  1. Finally, in the gesture recognizer function, when the gesture ends, re-enable the gestures again.
@objc func handlePan(_ gesture: UIPanGestureRecognizer) {
    guard let gestureView = gesture.view else {
        return
    }
    if gesture.state == .ended {
        setViewGestures(enabled: true, excludedView: gestureView)
    }
    // Do gesture stuff
}

That's it! The gestures are disabled while a single one is active and re-enables the gestures once the gesture ends.

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54