简体   繁体   中英

How to disable a “gesture recognizer” (XMCircleGestureRecognizer)?

For the app I'm building I'm using this class , and, as I recognize other gestures such as taps, swipes or long press without using apple gesture recognizer (I've built my own as these from Apple are to accurate) I'd like to be able to disable the XMCircleGestureRecognizer, and just enable it when I need it.

The problem is that when I'm calling it after some conditions, even if I lift my finger from the screen, and touch again the screen, it's still in the XMCircleGestureRecognizer, and never leave it. I've to kill the app and restart it.

I'm not sure that providing some of my code is necessary but if needed, just tell me (it's 600 lines long so it's pretty hard to get only the part you need).

Do you have any idea how I could do that ? Because I'm totally stuck on this point.

Thanks a lot for the help !

EDIT:

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)
    }

}

this is works for me:

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

Try this code. Basically, it will loop through all gestures you added to your view and remove them one by one.

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

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