如何禁用“手势识别器”(XMCircleGestureRecognizer)?

时间:2017-07-13 14:02:43

标签: ios swift swift3 uigesturerecognizer exit

对于我正在构建的应用程序我正在使用this class,并且,因为我认识到其他手势,例如点按,滑动或长按而不使用苹果手势识别器(我已经构建了自己的手势Apple是准确的)我希望能够禁用XMCircleGestureRecognizer,并在我需要时启用它。

问题在于,当我在某些条件下调用它时,即使我从屏幕上抬起手指,再次触摸屏幕,它仍然在XMCircleGestureRecognizer中,并且永远不会离开它。我要杀了应用程序并重新启动它。

我不确定提供我的一些代码是必要的,但如果需要,请告诉我(这是600行,所以很难只得到你需要的部分)。

你知道我怎么能这样做吗? 因为我完全坚持这一点。

非常感谢您的帮助!

编辑:

import UIKit

class ViewController: UIViewController {
var gesture: XMCircleGestureRecognizer?


override func viewDidLoad() {
    super.viewDidLoad()
    ascending = false
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    gesture?.isEnabled = false
    super.touchesBegan(touches, with: event)
    for touch in touches{
        let point = touch.location(in: self.view)
        for (index,finger)  in fingers.enumerated() {
            if finger == nil {
                fingers[index] = String(format: "%p", touch)

                if (finger1.isEmpty){
                    finger1 = [point.x, point.y]
                } else if (finger2.isEmpty){
                    finger2 = [point.x, point.y]
                } else if (finger3.isEmpty){
                    finger3 = [point.x, point.y]
                } else if (finger4.isEmpty){
                    finger4 = [point.x, point.y]
                } else if (finger5.isEmpty){
                    finger5 = [point.x, point.y]
                }
                break
            }
        }
    }

    timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(increaseValue), userInfo: nil, repeats: true)
    if ascending {
        ascending = false
    }
    else {
        ascending = true
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)

    for touch in touches {
        let point = touch.location(in: self.view)
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                switch (index){
                case 0 :
                    finger1 += [point.x, point.y]
                case 1 :
                    finger2 += [point.x, point.y]
                case 2 :
                    finger3 += [point.x, point.y]
                case 3 :
                    finger4 += [point.x, point.y]
                case 4 :
                    finger5 += [point.x, point.y]
                default :
                    break
                }
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    endTime = getCurrentMillis()
    gesture?.isEnabled = false

    for touch in touches {
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                fingers[index] = nil
                break
            }
        }
    }

    direction[0] = ""
    direction[1] = ""
    direction[2] = ""
    direction[3] = ""
    direction[4] = ""

    if finger1.count != 0 {
        direction[0] = GestureRecognizer(coordinates: finger1, index: 0)
    }
    if finger2.count != 0 {
        direction[1] = GestureRecognizer(coordinates: finger2, index: 1)
    }
    if finger3.count != 0 {
        direction[2] = GestureRecognizer(coordinates: finger3, index: 2)
    }
    if finger4.count != 0 {
        direction[3] = GestureRecognizer(coordinates: finger4, index: 3)
    }
    if finger5.count != 0 {
        direction[4] = GestureRecognizer(coordinates: finger5, index: 4)
    }

if Int64(endTime - startTime) < 400 {
// HERE I TEST MY GESTURES
}
}

func increaseValue() -> Int {
// HERE I TEST FOR LONG PRESS
else if !finger1.isEmpty && abs(finger1[finger1.count - 2] - finger1[0]) >= 40 && abs(finger1[1] - finger1[finger1.count - 1]) >= 40 {

        gesture = XMCircleGestureRecognizer(midPoint: self.view.center, target: self, action: #selector(ViewController.rotateGesture(recognizer:)))
        self.view.addGestureRecognizer(gesture!)

    }
    return value
}

func rotateGesture(recognizer:XMCircleGestureRecognizer)
{
    StatusLabel.text = ""

    if let rotation = recognizer.rotation {
        currentValue += rotation.degrees / 360 * 100
        StatusLabel.text = StatusLabel.text! + String(format:"Value: %.2f%%", currentValue)
    }

    if let angle = recognizer.angle {
        StatusLabel.text = StatusLabel.text! + "\n" + String(format:"Angle: %.2f%", angle.degrees)
    }

    if let distance = recognizer.distance {
        StatusLabel.text = StatusLabel.text! + "\n" + String(format:"Distance: %.0f%", distance)
    }

}

2 个答案:

答案 0 :(得分:3)

这对我有用:

    let gesture = XMCircleGestureRecognizer(midPoint: self.view.center, target: self, action: #selector(ViewController.rotateGesture(recognizer:)))
    self.view.addGestureRecognizer(gesture)        
    gesture.isEnabled = true //or false

答案 1 :(得分:0)

试试这段代码。基本上,它会遍历您添加到视图中的所有手势,并逐个删除它们。

if let gestureArr = theView.gestureRecognizers {
    for gesture in gestureArr {
        theView.removeGestureRecognizer(gesture)
    }
}