以编程方式制作的按钮与viewController的手势识别器冲突

时间:2018-10-02 02:49:48

标签: ios swift sprite-kit skspritenode

请参阅我的最后一个问题:

Spritekit: passing from UIButtons to buttons as SKSpriteNode

我正在开发SpriteKit游戏:用户可以点击并在屏幕上滑动以在场景内移动精灵,为此我在ViewController中添加了手势识别器。 然后,我创建一个HUD,以编程方式保留4个按钮,用户可以使用这些按钮向场景中添加其他精灵。 我希望我的按钮在按下时淡出并缩放一点,然后再返回到原始状态,但似乎它们与viewController的手势识别器冲突:按钮淡出并缩小,但它们保持该状态,不要返回到正常状态。 我能做什么? 这是Button类:

import SpriteKit
protocol ButtonDelegate: NSObjectProtocol {
    func buttonClicked(sender: Button)
}
class Button: SKSpriteNode {
    weak var delegate: ButtonDelegate!
    var buttonTexture = SKTexture()
    init(name: String) {
        buttonTexture = SKTexture(imageNamed: name)
        super.init(texture: buttonTexture, color: .clear, size: buttonTexture.size())
        self.isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
var touchEndedCallback: (() -> Void)?
weak var currentTouch: UITouch?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if isUserInteractionEnabled {
        setScale(0.9)
        self.alpha = 0.5
        if let currentTouch = touches.first {
            let touchLocation = currentTouch.location(in: self)
            for node in self.nodes(at: touchLocation) {
                delegate?.buttonClicked(sender: self)
            }
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    setScale(1.0)
    self.alpha = 1.0
    touchEndedCallback?()
    print("tapped!")
}
}

这是我在View Controller中使用的代码:

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        let skView = view as! SKView
        skView.isMultipleTouchEnabled = false
        skView.presentScene(scene)
    @IBAction func didTap(_ sender: UITapGestureRecognizer) {
        game.myCode
    }
    @IBAction func didPan(_ sender: UIPanGestureRecognizer) {
        let currentPoint = sender.translation(in: self.view)
        if let originalPoint = panPointReference {
            if abs(currentPoint.x - originalPoint.x) > (SquareSize * 0.9) {
                if sender.velocity(in: self.view).x > CGFloat(0) {
                    //game.myCode
                    panPointReference = currentPoint
                } else {
                    //game.myCode
                    panPointReference = currentPoint
                }
            }
        } else if sender.state == .began {
            panPointReference = currentPoint
        }
    }
    @IBAction func didSwipe(_ sender: UISwipeGestureRecognizer) {
        //game.myCode
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive shouldReceiveTouch: UITouch) -> Bool {
        if UITouch .isKind(of: Button.self) {
            return false
        }
        return true
    }
    func buttonClicked(sender: Button) {
        //myCode
    }
}

2 个答案:

答案 0 :(得分:1)

  

解决了这个问题!
  好的,这比我想象的要简单。
  (我休息了)。
  -我删除了UIgestures并以编程方式添加了它们;
  -在GameScene的touchesEnded方法中,我创建了一个条件来检查我的哪个节点已被触摸。
  -在View Controller中,我添加了
gestureRecognizer(_:shouldRecognizeSimultaneouslyWith :)方法,以避免与附加到不同视图的手势识别器发生冲突(我的节点位于两个不同的视图中):

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if gestureRecognizer.view != otherGestureRecognizer.view {
        return false
    }
    return true
}


我希望这会有所帮助...

答案 1 :(得分:0)

我不认为它们会干扰View Controller的内置手势识别器,因为您没有添加自己的手势,而只是覆盖了touchesBegin和touchesEnded。

“点击”会被打印出来吗?

touchesEnd()可能没有被调用,也可以尝试实现touchesCancelled()并查看是否被调用。