处理多个 GestureRecognizer [英] Handling Multiple GestureRecognizers

查看:24
本文介绍了处理多个 GestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 UIGestureRecognizers 时遇到了问题.我现在的目标是拥有一组 GestureRecognizers 来完成不同的任务,例如:

I've run into an issue understanding UIGestureRecognizers. My goal right now is to have a set of GestureRecognizers to do different tasks, for example:

override func viewDidLoad() {
    mainScene = GameScene(size: self.view.bounds.size)
    main = view as! SKView

    mainScene.panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(shiftView(recognizer:)))
    main.addGestureRecognizer(mainScene.panRecognizer)

    mainScene.tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(testTap(recognizer:)))
    main.addGestureRecognizer(mainScene.tapRecognizer)

    mainScene.pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(zoomView(recognizer:)))
    main.addGestureRecognizer(mainScene.pinchRecognizer)

这是我的游戏视图控制器,我在其中处理诸如平移地图、缩放和点击地图图块等操作.但我也希望能够使用 UITapGestureRecognizer 移动精灵,所以我也在我的 GameScene 中创建了这个:

This is my game View Controller where I handle actions such as panning around a map, zooming, and tapping on map tiles. But I also want to be able to move sprites with a UITapGestureRecognizer so I also created this in my GameScene:

if startGame == true{
            self.startGame()
            for node in (self.tempGameBoard.landShipLayer.children as? Array<landship>)! {

                node.landShipInteraction = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
                parentViewController.view.addGestureRecognizer(node.landShipInteraction)
            }
        }

在这种情况下,landShip 代表屏幕上的精灵,我想通过手势识别器与之交互.

The landShip in this case is representative of a sprite on screen that I would like to interact with via gesture recognizers.

我的问题是,如果我添加第二组识别器,点击操作将变得完全无响应.我仍然可以缩放和平移,但我希望在我的地图图块上的点击行为不会发生.我觉得好像我对手势识别器的工作原理缺少一些了解.

My issue is that if I add this second set of recognizers, the tapping action becomes completely unresponsive. I can still zoom and pan, but the tapping behaviors I expect on my map tiles do not occur. I feel as though I am missing some understanding of how the gesture recognizers work.

有什么想法吗?

谢谢!

推荐答案

UIGestureRecognizerDelegate 有一个特殊的功能,可以管理同一对象上的多个手势的同时识别,这可以解决问题.

The UIGestureRecognizerDelegate has a special function managing simultaneous recognition of several gestures on the same object, that will do the trick.

1) 设置您的 UIViewController 以符合 UIGestureRecognizerDelegate

1) Set your UIViewController to conform UIGestureRecognizerDelegate

2) 实现以下功能:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    if (gestureRecognizer == mainScene.panRecognizer || gestureRecognizer == mainScene.pinchRecognizer) && otherGestureRecognizer == mainScene.tapRecognizer {
        return true
    }
    return false
}

在这个特定的例子中,我们允许点击手势与平移和捏合同时触发.

In this particular example we allow the tap gesture to get triggered simultaneously with panning and pinching.

3) 然后只需将代表分配给平移和捏合手势识别器:

3) Then just assign the delegates to the pan and pinch gesture recognizers:

override func viewDidLoad() {
    // your code...

    // Set gesture recognizers delegates
    mainScene.panRecognizer.delegate = self
    mainScene.pinchRecognizer.delegate = self
}

这篇关于处理多个 GestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆