UIBezierPath原点问题和不同的变换

没当我们执行矩阵变换,执行的结果不是我们期望的结果。
如下创建个路径并旋转:

[path applyTransform:CGAffineTransformMakeRotation(M_PI / 9)];
UIBezierPath原点问题和不同的变换_第1张图片
图片.png

解决方案:

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

计算终点

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

你可能感兴趣的:(UIBezierPath原点问题和不同的变换)