I was wondering if there was any way to combine a CGRect with another CGRect to get a new CGRect. Does swift have any preset functionality to do this or is there another way of achieving this?
我想知道是否有任何方法可以将CGRect与另一个CGRect结合起来以获得新的CGRect。 swift是否有任何预设功能可以执行此操作,还是有其他方法可以实现此目的?
2 个解决方案
#1
11
let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 40, y: 40, width: 150, height: 150)
CGRectUnion(rect1, rect2) // {x 0 y 0 w 190 h 190}
See for more:
了解更多:
#2
4
Swift 3 and newer:
Swift 3和更新版本:
let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 40, y: 40, width: 150, height: 150)
let union = rect1.union(rect2) // {x 0 y 0 w 190 h 190}
Result is standardized, so resulting width and height are always positive:
结果是标准化的,因此产生的宽度和高度始终为正:
let rect3 = CGRect(x: 0, y: 0, width: -100, height: -100)
let clone = rect3.union(rect3) // {x -100 y -100 w 100 h 100}
Documentation: https://developer.apple.com/documentation/coregraphics/cgrect/1455837-union
#1
11
let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 40, y: 40, width: 150, height: 150)
CGRectUnion(rect1, rect2) // {x 0 y 0 w 190 h 190}
See for more:
了解更多:
#2
4
Swift 3 and newer:
Swift 3和更新版本:
let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 40, y: 40, width: 150, height: 150)
let union = rect1.union(rect2) // {x 0 y 0 w 190 h 190}
Result is standardized, so resulting width and height are always positive:
结果是标准化的,因此产生的宽度和高度始终为正:
let rect3 = CGRect(x: 0, y: 0, width: -100, height: -100)
let clone = rect3.union(rect3) // {x -100 y -100 w 100 h 100}
Documentation: https://developer.apple.com/documentation/coregraphics/cgrect/1455837-union