【问题标题】:Returning coordinates from touches in Swift从 Swift 中的触摸返回坐标
【发布时间】:2020-04-07 12:19:04
【问题描述】:

我目前正在用 Swift 编写 PONG 游戏。我希望玩家的拨片由玩家的手指控制,因此无论玩家在屏幕上拖动手指时,拨片都会移动。我希望能够找出玩家手指触摸的坐标,以便实现这一点。通过谷歌搜索和堆栈溢出,我发现了这个函数:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch = touches.anyObject()! as UITouch
        let location = touch.locationInView(self.view)
    }

虽然我不太确定它是做什么的,它是如何做的以及如何从中获取触摸的 x 和 y 值。如果有人能告诉我如何获取用户的 x/y 坐标,我将不胜感激!我希望最终结果与此类似:

PlayerPaddle.center.x = XTouchCoordinate

非常感谢!!

【问题讨论】:

  • 使用 UIPanGestureRecognizer
  • 请说明我将如何使用它以及我需要做什么来获取用户触摸的坐标。 (我很抱歉,因为我对 Swift 很陌生,很难理解如何使用它:()

标签: swift touch coordinates


【解决方案1】:

对于拖动使用 UIPanGestureRecognizer 对于获取 x 和 y 坐标的原始问题,您可以使用以下内容:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
    let touch = touches.first as! UITouch
    let location = touch.locationInView(self.view)
    var xcoord = location.x
    println(location.y)
}

【讨论】:

    【解决方案2】:

    如果您使用的是 GoogleMap SDK

     extension TargetViewController: GMSMapViewDelegate {
    
        func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)    
          {
    
            print("Tapped at Latitude: " + String(coordinate.latitude) + ", Longitude: "
                    + String(coordinate.longitude))
          }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多