3

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.

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

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

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31

3 Answers3

4

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
Kamran
  • 14,987
  • 4
  • 33
  • 51
0

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
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
0

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

let cgPoint = imgPin.convert(imgPin.frame.origin, to: self.view)
Thanh Vu
  • 1,599
  • 10
  • 14