Get various coordinates for touches in swift

swift detect touch on uiview
get touch location swift

Im trying to get the coordinates of various points when touching in a UIView for swift 4. I've seen the other post regarding a similar problem, but that code only allows the first touch to be registered. I would appreciate some help. Thanks.

This code will give you coordinates for every time you touch the screen. You can test it by print it out or put it straight in to a label.

@IBOutlet weak var imageView: UIImageView!
var coordinates = CGPoint.zero

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first{
        coordinates = touch.location(in: imageView)
        print(coordinates)
        textLabel.text = "\(coordinates)"
    }
}

How to find a touch's location in a view with location(in:), When the user starts touching an iOS screen, touchesBegan() is You can if you want pass a different view to location(in:) , and it will tell you  Swift – Get Users Location GPS Coordinates Apr 17, 2017 | Posted by Andrew | ios , swift , tutorial , xcode | Most Mapping libraries such as Google Maps and Apple Maps show you where the user currently is and also allows you to get their coordinates.

So, I was also able to find an answer for my question: I used the code bellow inside of a UIViewController to get a 2d array of integers when touching in different locations. Thanks for all the help.

var positionArray = Array(repeating: Array(repeating: 0, count: 2), count: 10)
    var counter = 0
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.view)
            let locx = Int(position.x)
            let locy = Int(position.y)
                positionArray[counter] = [locx, locy]
                print(positionArray[counter])
                counter = counter + 1
        }
    }

location(in:), iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+. Framework. UIKit. On This in its own coordinate system. Pass nil to get the touch location in the window's coordinates. The application will display the X and Y coordinates of the touch and update these values in real-time as the finger moves across the screen. When the finger is lifted from the screen, the start and end coordinates of the gesture will then be displayed on two label objects in the user interface.

You can implement the touchesMoved callback on your UIView subclass to do this.

func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)

This function gets called repeatedly while the touch is ongoing.

Here's the general idea:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else {
        return
    }

    let location = touch.location(in: self)
    print("x = \(location.x), y = \(location.y)")
}

location(ofTouch:in:), Returns the location of one of the gesture's touches in the local coordinate system of a given view. SDKs. iOS Getting the Touches and Location of a Gesture. Get all coordinates between two points Swift 5 swiftdevcenter_admin 06/30/2019 06/30/2019 2 Comments on Get all coordinates between two points Swift 5 There are an infinite number of coordinates between two points, so we are going to build a formula so that you can get your coordinates in an array as per your uses.

How to get the Touch position on iOS device?, The touches began gets called whenever you touch anywhere on the screen. This gives us touch event that contains all the touches. We are  Swift version: 5.1. Paul Hudson @twostraws May 28th 2019. When the user starts touching an iOS screen, touchesBegan () is immediately called with a set of UITouches. If you want to find where the user touched, you need to take one of those touches then use location (in:) on it, like this: override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let position = touch.location(in: view) print(position) } }

An Example Swift iOS 10 Touch, Multitouch and Tap Application, Topics covered in this chapter include the detection of single and multiple taps and touches,  Swift - Get Users Location GPS Coordinates - Duration: 12:50. Seemu Apps 18,055 views

Migrating to Swift from Flash and ActionScript, The touchesBegan function supports multitouch (i.e., the user tapping the screen with To get the touch location in scene coordinates we call SKNode's helper  The view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify self to get the touch location in its own coordinate system. Pass nil to get the touch location in the window’s coordinates.

Comments
  • Thanks! Does this work when you stop touching and then touch in another place?
  • Yes. If you stop touching then you get a touchesEnded callback (if you have implemented it). Then when you touch another place you get a touchesBegan and then more touchesMoved.
  • I am sorry for contacting you this way (my comment isnt related to your input here). I came across a question you voted on in "triage" where you made the wrong choice. Please: study the help for triage carefully, to avoid putting items into the edit queue that don't belong there. I hope you see this as a chance to improve your voting. I am specifically talking about stackoverflow.com/review/triage/21317141. Feel free to drop me a comment in case you have further questions or feedback for me. If you give me a short heads up, I will quickly delete this comment.
  • Hi, ncke. I gave the mark to Daniel because of the more detailed explanation -and because I apparently don't know how to use a touchesEnded-, but they both appear to work.