SwiftCGRectExtensions 是 CGRect, CGPoint 和 CGSize 简便函数的集合,使用 Swift 编写,支持 iOS 和 OS X。
CGRect
CGPoint
CGSize
SwiftCGRectExtensions 目标是为普遍使用的 rect 操作提供清晰的函数。
Operators for adding, scaling sizes, applying CGAffineTransform
CGAffineTransform
Shortcut properties for corner and center points
Offsetting, insetting and extending methods with multiple ways of specifying arguments
Aligning and centering sizes in rects
Copying rects with some values changed without going through a variable
Respects the different coordinate systems on iOS and OS X
Unit tested
示例:
import Foundation import CGRectExtensions let rect = CGRect(1, 2, 100, 200) // shorter constructor let minY = rect.minY // shortcut properties let topCenter = rect.topCenter // OS-dependent coordinate system let oppositeOrigin = rect.origin + rect.size // adding let corneredRect = rect.with(x: 0, y: 0) // modified copy let nextPageRect = rect.rectByOffsetting(dx: 100) // offsetting let paddedRect = rect.rectByInsetting(top: 66, left: 10, right: 10) // insetting let quarterSize = rect.size * 0.5 // scaling sizes let corner = rect.rectByAligning(quarterSize, corner: .MinXEdge, .MinYEdge) // aligning sizes let halfWidthSize = rect.size * (0.5, 1) // scaling sizes let centeredRect = rect.rectByCentering(halfWidthSize) // centering sizes let scaledRect = rect * CGAffineTransformMakeScale(2.0, 3.0)