如何将 CGRect 转换为 CGPoint iOS Swift

How to convert CGRect to CGPoint iOS Swift

这里我想将UIImageViewmaxYCGRect值转换为CGPoint,但是当我试图将CGRect值转换为CGPoint 我有一个错误。

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

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

您必须传递 CGPoint 而不是 CGFloat ,如下所示,

// 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)

您可以按原样传递 CGRect 并获得分数 origin,

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

您可以使用此扩展程序:

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) }
}

然后你可以像这样使用它:

let cgPoint = imgPin.frame.bottomTrailingPoint

你可以这样将点数从 imgPin 转换为 self.view

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