如何禁用“手势识别器”(XMCircleGestureRecognizer)?
Posted
技术标签:
【中文标题】如何禁用“手势识别器”(XMCircleGestureRecognizer)?【英文标题】:How to disable a "gesture recognizer" (XMCircleGestureRecognizer)? 【发布时间】:2017-07-13 14:02:43 【问题描述】:对于我正在构建的应用程序,我使用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)
【问题讨论】:
【参考方案1】:这对我有用:
let gesture = XMCircleGestureRecognizer(midPoint: self.view.center, target: self, action: #selector(ViewController.rotateGesture(recognizer:)))
self.view.addGestureRecognizer(gesture)
gesture.isEnabled = true //or false
【讨论】:
那是最简单的方法,但是你在哪里声明了手势?在 viewDidLoad 中?因为我不能在 touchesEnded() 等另一个函数中执行 .isEnable 方法 您可以将属性“gesture”设为全局并在代码的任何部分使用。 好的,看起来可以。但问题不是我想的那个。在我的视图控制器中,我创建了 XMCircleGestureRecognizer,这没问题。但是当 touchesEnded 发生时,什么也没有发生,它永远不会离开并返回到 viewController,所以我不能禁用它。你能帮忙出个主意吗?善良:) 你能给我更多的信息或有这个问题的示例应用吗? 刚刚用一些代码编辑了我的问题。如果您需要更多信息,请告诉我。我没有在 XMCircleGestureRecognizer 中进行任何修改。正如我告诉过你的,它卡在 XMCircleGestureRecognizer 的 overrode touchesEnded 中【参考方案2】:试试这个代码。基本上,它会循环遍历您添加到视图中的所有手势并一一移除。
if let gestureArr = theView.gestureRecognizers
for gesture in gestureArr
theView.removeGestureRecognizer(gesture)
【讨论】:
以上是关于如何禁用“手势识别器”(XMCircleGestureRecognizer)?的主要内容,如果未能解决你的问题,请参考以下文章
如何在UITextField和UISearchBar中禁用长按手势识别器?