Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the coordinates from the location I touch the touchscreen

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?

like image 615
Lukas Köhl Avatar asked Nov 09 '14 14:11

Lukas Köhl


People also ask

How do I get touch coordinates on Android?

You can retrieve the coordinates by using getX() and getY() .

How do I get touch location in Swift?

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.

How to lookup latitude and longitude of my location?

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?

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...

What is the latitude and longitude app?

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.

How do I find the coordinates of an address?

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.


4 Answers

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)
}
like image 167
Mundi Avatar answered Oct 09 '22 08:10

Mundi


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

like image 21
LittleNose Avatar answered Oct 09 '22 07:10

LittleNose


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)

    }
}
like image 15
kb920 Avatar answered Oct 09 '22 08:10

kb920


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

like image 3
Mike Lee Avatar answered Oct 09 '22 09:10

Mike Lee