如何在 swift 中创建将接收者作为参数的 sorted() 之类的方法?
How to create methods like sorted() that takes the receiver as a parameter in swift?
我写了一个 CGRect class 的扩展。我添加了一种按系数缩放 CGRect 的方法。但是我不知道如何写它才能实现
let newFrame = view.frame.scale(by: 0.8)
它 returns 一个 CGRect 被设置为 newFrame。
到目前为止我已经尝试了什么..
func scale(_ input: CGRect, byFactor factor: CGFloat) -> CGRect {
let newCentre = input.centre
let newHeight = input.size.height * factor
let newWidth = input.size.width * factor
let newSize = CGSize(width: newWidth, height: newHeight)
let newRect = CGRect(centre: newCentre, size: newSize)
return newRect
}
感谢任何帮助。谢谢!
编辑:
var upperLeft: CGPoint { return CGPoint(x: minX, y: minY) }
var centre: CGPoint { return CGPoint(x: upperLeft.x + size.width/2, y: upperLeft.y + size.height/2) }
init(centre: CGPoint, size: CGSize) {
let upperLeft = CGPoint(x: centre.x-size.width/2, y: centre.y-size.height/2)
self.init(origin: upperLeft, size: size)
}
我在扩展中实现了什么
class、结构或枚举的扩展中的接收器由 self
表示
extension CGRect {
func scale(byFactor factor: CGFloat) -> CGRect {
let newCentre = self.centre
let newHeight = self.height * factor // CGRect has width and height properties
let newWidth = self.width * factor
let newSize = CGSize(width: newWidth, height: newHeight)
let newRect = CGRect(centre: newCentre, size: newSize)
return newRect
}
}
PS:centre
的一个更简单的实现是
var centre : CGPoint {
return CGPoint(x:CGRectGetMidX(self), y:CGRectGetMidY(self))
}
扩展 CGRect 以缩放具有相同中心的矩形:
extension CGRect {
mutating func scale(factor: CGFloat) {
let newHeight = self.size.height * factor
let newWidth = self.size.width * factor
let newX = self.origin.x + (self.height - newHeight) / 2
let newY = self.origin.y + (self.width - newWidth) / 2
self = CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
}
}
用法:
view.frame.scale(0.8)
我写了一个 CGRect class 的扩展。我添加了一种按系数缩放 CGRect 的方法。但是我不知道如何写它才能实现
let newFrame = view.frame.scale(by: 0.8)
它 returns 一个 CGRect 被设置为 newFrame。
到目前为止我已经尝试了什么..
func scale(_ input: CGRect, byFactor factor: CGFloat) -> CGRect {
let newCentre = input.centre
let newHeight = input.size.height * factor
let newWidth = input.size.width * factor
let newSize = CGSize(width: newWidth, height: newHeight)
let newRect = CGRect(centre: newCentre, size: newSize)
return newRect
}
感谢任何帮助。谢谢!
编辑:
var upperLeft: CGPoint { return CGPoint(x: minX, y: minY) }
var centre: CGPoint { return CGPoint(x: upperLeft.x + size.width/2, y: upperLeft.y + size.height/2) }
init(centre: CGPoint, size: CGSize) {
let upperLeft = CGPoint(x: centre.x-size.width/2, y: centre.y-size.height/2)
self.init(origin: upperLeft, size: size)
}
我在扩展中实现了什么
class、结构或枚举的扩展中的接收器由 self
extension CGRect {
func scale(byFactor factor: CGFloat) -> CGRect {
let newCentre = self.centre
let newHeight = self.height * factor // CGRect has width and height properties
let newWidth = self.width * factor
let newSize = CGSize(width: newWidth, height: newHeight)
let newRect = CGRect(centre: newCentre, size: newSize)
return newRect
}
}
PS:centre
的一个更简单的实现是
var centre : CGPoint {
return CGPoint(x:CGRectGetMidX(self), y:CGRectGetMidY(self))
}
扩展 CGRect 以缩放具有相同中心的矩形:
extension CGRect {
mutating func scale(factor: CGFloat) {
let newHeight = self.size.height * factor
let newWidth = self.size.width * factor
let newX = self.origin.x + (self.height - newHeight) / 2
let newY = self.origin.y + (self.width - newWidth) / 2
self = CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
}
}
用法:
view.frame.scale(0.8)