简体   繁体   English

暂时禁用手势识别器滑动功能

[英]Temporarily disable gesture recognizer swiping functionality

I have a uiview with multipe buttons and images on it.我有一个 uiview,上面有多个按钮和图像。 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 是的,有一个UIGestureRecognizer的委托方法你可以设置如下的委托方法

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. 根据文档,所有UIGestureRecognizer类对象都具有enabled属性。

What it does is: 它的作用是:

Disables a gesture recognizers so it does not receive touches. 禁用手势识别器,使其不接收触摸。 The default value is true. 默认值是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. 如果在手势识别器当前正在识别手势时将此属性更改为false,则手势识别器将转换为已取消状态。

你可以做点什么,

 gestureBack.enabled = false

TL;DR: You need to disable the gesture. TL;DR:您需要禁用手势。

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.创建了一个 function,它将启用/禁用 controller 中除所选视图之外的所有视图上的手势。
/// 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:接下来,在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.最后,在手势识别器 function 中,当手势结束时,再次重新启用手势。
@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.手势在单个处于活动状态时被禁用,并在手势结束后重新启用手势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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