从多个点击中获取坐标并将它们 return 放入数组中以供在另一个函数中使用
Get coordinates from multiple taps and return them in an array for use in another function
我在我的应用程序上设置了一个空的 UIView。我希望能够点击视图上的两个点,获取点击的坐标,并将它们附加到数组中。然后我想将这个数组传递给另一个函数使用(在它们之间画一条线)。我知道如何访问这些位置,但我不知道如何 return 将它们提供给另一个函数使用。这是我目前所拥有的:
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)
}
我无法修改 touchesBegan 函数,使其可以 return 数组。如果我尝试这样做,它将不再覆盖 UIKit 的 touchesBegan 函数并且 Xcode 将把它作为我自己的函数来读取。谢谢!
我认为您根本不需要 return 它们。您的坐标数组是 class 上的 属性,您应该能够在 class 内的任何位置访问它,甚至可以通过您的 superclass 访问它。如果需要private或者fileprivate,那么可以根据需要使用delegates传递信息backwards/upwards。
我在我的应用程序上设置了一个空的 UIView。我希望能够点击视图上的两个点,获取点击的坐标,并将它们附加到数组中。然后我想将这个数组传递给另一个函数使用(在它们之间画一条线)。我知道如何访问这些位置,但我不知道如何 return 将它们提供给另一个函数使用。这是我目前所拥有的:
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)
}
我无法修改 touchesBegan 函数,使其可以 return 数组。如果我尝试这样做,它将不再覆盖 UIKit 的 touchesBegan 函数并且 Xcode 将把它作为我自己的函数来读取。谢谢!
我认为您根本不需要 return 它们。您的坐标数组是 class 上的 属性,您应该能够在 class 内的任何位置访问它,甚至可以通过您的 superclass 访问它。如果需要private或者fileprivate,那么可以根据需要使用delegates传递信息backwards/upwards。