Scott
Scott

Reputation: 147

How can I get CGPoint?

I'd like to obtain CGPoint of UIView, but I could not.

What is the correct code?

//My Code
let wayPoint = vMapObject.point
print(wayPoint) //(Function)

I want the X and Y coordinates as a tuple type.

Upvotes: 3

Views: 2779

Answers (1)

technerd
technerd

Reputation: 14514

You can try this code to get x and y point for view as tuple.

func getPointForView(_ view : UIView) -> (x:CGFloat,y:CGFloat)
{
    let x = view.frame.origin.x
    let y = view.frame.origin.y
    return (x,y)
}

From your code I guess, vMapObject is your view. So declared it as UIView.

var vMapObject : UIView!

Make call for that view as below:

let point = getPointForView(vMapObject)
print("X : \(point.x)")
print("Y : \(point.y)")

Upvotes: 2

Related Questions