【问题标题】:UITapGesture and TouchesBegan are producing different locations in viewTap Gesture 和 TouchesBegan 在视图中生成不同的位置
【发布时间】:2017-08-06 15:52:44
【问题描述】:

我正在使用 SKTileMapsCameraNode 以及 GameplayKitSpriteKit

我想要工作的代码不正确在“视图”中找到位置

func handleTapFrom(_ sender: UITapGestureRecognizer)
{
    let location = sender.location(in: self.view)
    if (map.contains(location))
    {
        let tRow = map.tileRowIndex(fromPosition: location)
        let tColumn = map.tileColumnIndex(fromPosition: location)
        let movePosition = map.centerOfTile(atColumn: tColumn, row: tRow)
        let moveAction = SKAction.move(to: movePosition, duration:1.0)
        cam.run(moveAction)
    }

}    

正常工作的代码是在self(而不是self.view)中查找位置,这是正确的

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
{
    let firstTouch = touches.first?.location(in: self)
    if (map.contains(firstTouch!))
    {
        let tRow = map.tileRowIndex(fromPosition: firstTouch!)
        let tColumn = map.tileColumnIndex(fromPosition: firstTouch!)
        let movePosition = map.centerOfTile(atColumn: tColumn, row: tRow)
        let moveAction = SKAction.move(to: movePosition, duration:1.0)
        cam.run(moveAction)
    }
}

列和行根据位置是正确的,我遇到的问题是两个函数之间的位置(xfloat,yfloat)不同,即使触摸在同一位置

这两段代码都在 GameScene 类中,但我不明白为什么它们的位置不同?以及如何修复点击手势,使其找到的位置与 touchesBegan 中的位置相同。

对不起,如果这令人困惑或难以理解。我欢迎有机会澄清。

【问题讨论】:

    标签: swift sprite-kit uitapgesturerecognizer gamekit touchesbegan


    【解决方案1】:

    在您的handleTapFrom 函数中,位置应来自convertPoint

    func handleTapFrom(_ sender: UITapGestureRecognizer) {
        if sender.state != .ended {
            return
        }
    
        let location = sender.location(in: sender.view!)
        let targetLocation = self.convertPoint(fromView: location)
    
        if map.contains(targetLocation) {
            // Your code here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多