Getting the coordinates from the location I touch the touchscreen
Asked Answered
D

6

30

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?

Developer answered 9/11, 2014 at 14:34 Comment(1)
Another solution is UITapGestureRecognizer, see: https://mcmap.net/q/472170/-how-to-get-tap-point-coordinates-in-ios-swiftChandler
G
47

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)
}
Glissando answered 9/11, 2014 at 14:41 Comment(3)
And if I want to do it in the ViewController class I get the error 'ViewController' is not convertible to 'UIView' What should I change in the code?Hally
@Glissando is right, and just to clarify, your location line would look like the following if you are getting the touch location from the view controller and not a UIView subclass: let location = touch.locationInView(self.view)Merv
could someone update this for Swift 5 and Swift UI, please?Delija
N
22

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

Nude answered 2/10, 2016 at 16:11 Comment(1)
Thanks for answering. I already got that figured out but I also appreciate the easier solution.Hally
W
15

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)

    }
}
Woodpecker answered 12/1, 2016 at 4:21 Comment(0)
I
3

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

Iodate answered 6/2, 2018 at 1:55 Comment(0)
D
2

For SwiftUI I created a new swift file called HostingController.swift

import Foundation
import UIKit
import SwiftUI

class HostingController: UIHostingController<ContentView> {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: view)
            print(position)
        }
    }
}

Then I changed the following lines of code in the SceneDelegate.swift

window.rootViewController = UIHostingController(rootView: ContentView())

to

window.rootViewController = HostingController(rootView: ContentView())

SwiftUI is basically wrapped in a ViewController via the UIHostingController. At least thats how I think it is.

I hope this helps!

Greetings krjw

Delija answered 18/7, 2019 at 9:22 Comment(0)
Q
0

Latest swift4.0, For ViewController

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
    let location = touch.location(in: self.view)
    print(location.x)
    print(location.y)
  }
}
Quipu answered 31/1, 2019 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.