沒當(dāng)我們執(zhí)行矩陣變換逻杖,執(zhí)行的結(jié)果不是我們期望的結(jié)果梭灿。
如下創(chuàng)建個(gè)路徑并旋轉(zhuǎn):
[path applyTransform:CGAffineTransformMakeRotation(M_PI / 9)];
解決方案:
func RotatePath(path:UIBezierPath, theta:CGFloat) {
let transform = CGAffineTransform.init(rotationAngle: theta)
ApplyCenteredPathTransform(path: path, transform: transform)
}
// Translate path’s origin to its center before applying the transform
func ApplyCenteredPathTransform(path :UIBezierPath,transform:CGAffineTransform) {
let center = PathBoundingCenter(path: path)
var t = CGAffineTransform.identity
t = t.translatedBy(x: center.x, y: center.y)
t = transform.concatenating(t)
t = t.translatedBy(x: -center.x, y: -center.y)
path.apply(t)
}
其他變換==================
偏移
func OffsetPath(path:UIBezierPath,offset:CGSize) {
let transform = CGAffineTransform.init(translationX: offset.width, y: offset.height)
ApplyCenteredPathTransform(path: path, transform: transform)
}
// Scale path to sx, sy
func scalePath(path:UIBezierPath,size:CGSize) {
let transform = CGAffineTransform.init(scaleX: size.width, y: size.height)
ApplyCenteredPathTransform(path: path, transform: transform)
}
Flip horizontally
func MirrorPathHorizontally(path :UIBezierPath) {
let transform = CGAffineTransform.init(scaleX: -1, y: 1)
ApplyCenteredPathTransform(path: path, transform: transform)
}
Flip vertically
func MirrorPathVertically(path :UIBezierPath) {
let transform = CGAffineTransform.init(scaleX: -1, y: 1)
ApplyCenteredPathTransform(path: path, transform: transform)
}
計(jì)算終點(diǎn)
func PathBoundingCenter(path:UIBezierPath) -> CGPoint{
return RectGetCenter(rect: PathBoundingBox(path: path))
}
func PathBoundingBox(path:UIBezierPath) -> CGRect {
return path.cgPath.boundingBox
}
func RectGetCenter(rect:CGRect) -> CGPoint {
return CGPoint.init(rect.midX, rect.midY)
}