Rotate CGPath without changing its position

前端 未结 3 1323
小蘑菇
小蘑菇 2020-12-28 09:40

I want to rotate a CGPath, I\'m using the following code:

CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
CGPathRef         


        
3条回答
  •  鱼传尺愫
    2020-12-28 09:42

    A Swift 5 version:

    func rotate(path: UIBezierPath, degree: CGFloat) {
        let bounds: CGRect = path.cgPath.boundingBox
        let center = CGPoint(x: bounds.midX, y: bounds.midY)
    
        let radians = degree / 180.0 * .pi
        var transform: CGAffineTransform = .identity
        transform = transform.translatedBy(x: center.x, y: center.y)
        transform = transform.rotated(by: radians)
        transform = transform.translatedBy(x: -center.x, y: -center.y)
        path.apply(transform)
    }
    

提交回复
热议问题