steveSarsawa
steveSarsawa

Reputation: 1679

How to convert CGRect to CGPoint iOS Swift

Here i want to convert UIImageView's maxY CGRect value to CGPoint, but when i tried to convert CGRect value to CGPoint i got an error.

let cgPoint = imgPin.convert(imgPin.frame.minY, to: self.view)

Expression type '@lvalue CGRect' is ambiguous without more context

Upvotes: 3

Views: 2248

Answers (3)

Thanh Vu
Thanh Vu

Reputation: 1739

You can do like this to convert point from imgPin to self.view

let cgPoint = imgPin.convert(imgPin.frame.origin, to: self.view)

Upvotes: 0

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119312

You can use this extension:

extension CGRect {
    var topLeadingPoint: CGPoint { return CGPoint(x: minX, y: minY) }
    var topTrailingPoint: CGPoint { return CGPoint(x: maxX, y: minY) }
    var bottomLeadingPoint: CGPoint { return CGPoint(x: minX, y: maxY) }
    var bottomTrailingPoint: CGPoint { return CGPoint(x: maxX, y: maxY) }
}

Then you can use it like:

let cgPoint = imgPin.frame.bottomTrailingPoint

Upvotes: 0

Kamran
Kamran

Reputation: 15238

You have to pass a CGPoint instead of CGFloat as below,

// set x, y as per your requirements.
let point = CGPoint(x: imgPin.frame.minX, y: imgPin.frame.minY)
let cgPoint = imgPin.convert(point, to: self.view)

OR

You can pass the CGRect as it is and get the point as origin,

let cgPoint = v.convert(imgPin.frame, to: self.view).origin

Upvotes: 4

Related Questions