Search code examples
swiftuitapgesturerecognizeruitouchtouchesbegancgpoint

Get coordinates from multiple taps and return them in an array for use in another function


I have set up an empty UIView on my app. I would like to be able to tap two points on the view, get the coordinates of the taps, and append them to an array. Then I would like to pass this array to another function for use (to draw a line between them). I know how to access the locations, but I do not know how I can return them to another function for use. Here is what I have so far:

var coordinates = [CGPoint]()
override func touchesBegan (_ touches: Set <UITouch>, with event: UIEvent?){
let touch = touches.first!
let location= touch.locaition(in:self)
coordinates.append(location)
}

I am not able to modify the touchesBegan function so that it can return the array. If I try to, it will no longer be overriding UIKit's touchesBegan function and Xcode will be reading it as my own function. Thanks!


Solution

  • You don't need to return them at all I think. Your coordinates array is a property on your class and you should be able to access that anywhere inside your class, or even by your superclass. If it needs to be private or fileprivate, then you can use delegates to pass information backwards/upwards as needed.