简体   繁体   中英

Temporarily disable gesture recognizer swiping functionality

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

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
}

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.

你可以做点什么,

 gestureBack.enabled = false

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM