Home>

I want to create a function that has the process of acquiring the tapped coordinates and comparing which range the acquired coordinates fall into.

Get the coordinates below,

let touch = touches.first!
let location = touch.location (in: self.view)
let locationXY = (location.x, location.y)

I want to use the above coordinates in the Switch statement.

switch (variable that receives locationXY) {
case (x range, y range)
.
.
.
default:
}


However, I am thinking of implementing the above switch statement in a function.
However, I don't know how to make the function argument a (location.x, location.y) type.
I understand that the coordinate type is CGFloat, but I would like to set an argument that stores the x and y coordinates if possible.

func checkarea (_ Argument with the same value as locationXY: CGFloat?) {
  switch argument {
  case (x range, y range)
  default:
  }
}

In short, I want to know how to write arguments that can store (x coordinate, y coordinate).

If not possible, you can compare the x and y coordinates separately.

Please give me an answer.

  • Answer # 1

    1

    func checkarea (_ location: CGPoint) {

    2

    func checkarea (_ location: (CGFloat, CGFloat)) {

    3

    func checkarea (_ locationX: CGFloat, _ locationY: CGFloat) {