I try to get the coordinates from the location where I hit the touchscreen to do put a specific UIImage at this point.
How can I do this?
You can retrieve the coordinates by using getX() and getY() .
Basic Swift Code for iOS AppsStep 2 − Open Main. storyboard and add one label as shown below. On this label we will show the touch position. Step 3 − Create @IBOutlet for the label, name it touchPositionLabel.
To lookup latitude and longitude of my location or share my coordinates, simply enable the location on any browser, and it will show the exact address, lat long, and your current coordinates. Find my coordinates is a perfect tool to use when you are not sure where you are.
How to use our GPS Coordinates Finder? 1 Open this website, and it will automatically detects your location. 2 Make sure you click Allow (in Chrome) or Share Location (in Firefox). ... 3 We do not collect any of your location data. 4 You can also use GPS Coordinates Converter to convert any address to lat long, and vice versa. More items...
The Latitude and Longitude app allows you to get or share map coordinates of your desired location. You can share gps coordinates in many ways using Latitude Longitude app. Share your current gps location with anyone using GPS coordinates, address or both.
Coordinates Finder. The coordinates finder or coordinate locator will search for longitude and latitude on map. It will also lookup any address by clicking on the map. The result will show on both the map coordinates and on the corresponding fields.
In a UIResponder
subclass, such as UIView
:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject()! as UITouch
let location = touch.locationInView(self)
}
This will return a CGPoint
in view coordinates.
Updated with Swift 3 syntax
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.first!
let location = touch.location(in: self)
}
Updated with Swift 4 syntax
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: self.view)
}
Taking this forward for Swift 3 - I'm using:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self)
print(position.x)
print(position.y)
}
}
Happy to hear any clearer or more elegant ways to produce the same result
This is work in Swift 2.0
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let position :CGPoint = touch.locationInView(view)
print(position.x)
print(position.y)
}
}
Swift 4.0
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: view)
print(position)
}
}
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With