失败所有其他手势

时间:2015-05-23 10:03:20

标签: ios swift uigesturerecognizer

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

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

选项2:
另一种方法是获取所述视图的手势数组,并为阵列内的所有手势设置enabledfalse

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

1 个答案:

答案 0 :(得分:1)

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

这是完整的工作代码:

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


}

希望这就是你所需要的。