How to get user touch point/location

Vikas Kore
2 min readAug 19, 2019

In this story we will understand how to get user touch location/ CGPoint value and update view accordingly or do stuff.

In a Game development we frequently use touchesBegan(), touchesMoved(), touchesEnded(), touchesCancelled() to move object or to do any animation but rarely we use these functions in Business or Enterprise application development.

here we are using touchesMoved(), touchesEnded() function to get user tapped location on screen and identify which subview is at the location (CGPoint).

touchesMoved() : returns user touch location while moving finger on a view
touchesEnded() : returns touch location on touch end event, means where user removed or take off his touch/finger

We are considering following problem
How to update user score on label by selecting from given values ?
user could able to tap on score value or able move fingers to select correct score.

To implement this I have collection of buttons because we have to deselect all buttons before selecting one (assigned tag value to the buttons same as available integer value on button).

//Array of buttons
@IBOutlet var scoreOptions: [UIButton]!

The higher order(standard) function map help us to loop over all the elements in collection in a single line code.
In this example we are setting Green color border to button to show selected and using clear color for deselect button

//Clear all selections
_ = scoreOptions.map {$0.setBorder(withColor: .clear, borderWidth: 1.0, cornerRadius: button.frame.height/2) }
//set border to selected button
button.setBorder(withColor: .green, borderWidth: 1.0, cornerRadius: button.frame.height/2)

Thats it.

lets have a look at touchesMoved() function

/*
Animate user selection while moving finger on buttons
*/
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {if let touch = touches.first {let touchpoint:CGPoint = touch.location(in: self.view)if scoreView.frame.contains(touchpoint) {//returns touch location respective to score viewlet position = touch.location(in: scoreView)print(position)//For loop to find out which button is at touch locationfor button in scoreOptions {if button.frame.contains(position) {//Clear all selections_ = scoreOptions.map {$0.setBorder(withColor: .clear, borderWidth: 1.0, cornerRadius: button.frame.height/2) }//set border to selected buttonbutton.setBorder(withColor: .green, borderWidth: 1.0, cornerRadius: button.frame.height/2)//Pass button tag to functionscoreSelected(button.tag)}}}}}

touchesEnded()

/*
Animate user selection while tapping on a buttons
*/
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {if let touch = touches.first {let touchpoint:CGPoint = touch.location(in: self.view)if scoreView.frame.contains(touchpoint) {let position = touch.location(in: scoreView)print(position)for button in scoreOptions {if button.frame.contains(position) {_ = scoreOptions.map {$0.setBorder(withColor: .clear, borderWidth: 1.0, cornerRadius: button.frame.height/2) }button.setBorder(withColor: .green, borderWidth: 1.0, cornerRadius: button.frame.height/2)scoreSelected(button.tag)}}}}}

Function to update selected score on label

//Update selected value on label
func scoreSelected(_ id : Int) {
lblScore.text = String(id) // "\(id)"
}

For complete code: https://gist.github.com/bfb517282df666b598d07ca12c68dd62.git

Thank you.

--

--