美文网首页
UIBezierPath原点问题和不同的变换

UIBezierPath原点问题和不同的变换

作者: 代码干货 | 来源:发表于2017-09-27 16:20 被阅读192次

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

    [path applyTransform:CGAffineTransformMakeRotation(M_PI / 9)];
    
    图片.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原点问题和不同的变换

          本文链接:https://www.haomeiwen.com/subject/bqvuextx.html