gpt4 book ai didi

ios - 所有其他手势均失败

转载 作者:行者123 更新时间:2023-11-29 02:00:51 28 4
gpt4 key购买 nike

在我的容器 Controller 中,有时我必须向 View 添加手势识别器来处理事情。是否有可能使所述 View 上的所有其他手势都失败并仅执行添加的手势?

选项 1:
现在,当检测到新手势时,所有单独的手势都设置为失败,但这样做似乎效率不高。

选项 2:
另一种方法是获取所述 View 的手势数组,并将数组内的所有手势的 enabled 设置为 false

我想做的是将新手势的委托(delegate)设置到容器 Controller ,并使用其中一种委托(delegate)方法在检测到新手势时使所有其他手势失败。

最佳答案

如果您想删除其他手势识别器并希望保留用户使用的一个手势,那么您可以通过检测该手势的方向来完成,然后您可以删除所有其他手势并保留使用过的手势。

这是完整的工作代码:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector("rightSwiped:"))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("leftSwiped:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)

let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector("downSwiped:"))
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)

let swipeUp = UISwipeGestureRecognizer(target: self, action: Selector("upSwiped:"))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeUp)
}

func rightSwiped(recognizer: UISwipeGestureRecognizer)
{
println("right swiped ")
manageGesture(recognizer)

}

func manageGesture(recognizer: UISwipeGestureRecognizer) {

//First detect which gesture is used

if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
println("Right")

if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Right {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}

}else if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
println("Left")

if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}else if recognizer.direction == UISwipeGestureRecognizerDirection.Up {
println("Up")

if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Up {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}else if recognizer.direction == UISwipeGestureRecognizerDirection.Down {
println("Down")

if let recognizers = view.gestureRecognizers {
for recognizer in recognizers {
if recognizer.direction == UISwipeGestureRecognizerDirection.Down {
println("Called")
}else {
view.removeGestureRecognizer(recognizer as! UIGestureRecognizer)
}
}
}
}

}

func leftSwiped(recognizer: UISwipeGestureRecognizer)
{
println("left swiped ")
manageGesture(recognizer)
}

func downSwiped(recognizer: UISwipeGestureRecognizer)
{
println("down swiped ")
manageGesture(recognizer)
}

func upSwiped(recognizer: UISwipeGestureRecognizer)
{
println("Up swiped ")
manageGesture(recognizer)
}


}

希望这就是您所需要的。

关于ios - 所有其他手势均失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411378/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com