代码之家  ›  专栏  ›  技术社区  ›  Josef Affourtit

Uitap手势和触摸开始在视图中产生不同的位置

  •  0
  • Josef Affourtit  · 技术社区  · 9 年前

    我正在使用 SKTileMaps公司 摄像机节点 游戏工具包 和

    我想要的代码

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

    工作正常

    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)也不同

    抱歉,如果这让人困惑或无法理解。我欢迎有机会澄清。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Mark Brownsword    9 年前

    在你的 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
        }
    }
    
    推荐文章