从Swift的触摸中返回坐标

问题描述 投票:0回答:2

我目前正在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

非常感谢!!

swift touch coordinates
2个回答
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)
}

0
投票
 extension TargetViewController: GMSMapViewDelegate {

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)    
      {

        print("Tapped at Latitude: " + String(coordinate.latitude) + ", Longitude: "
                + String(coordinate.longitude))
      }

    }
© www.soinside.com 2019 - 2024. All rights reserved.