What is the best way to rotate a CGPoint on a grid?

后端 未结 6 1724
轻奢々
轻奢々 2020-12-17 17:49

I want to rotate a CGPoint on the screen depending on the angle and the rotation is anchored on another point. Was wondering what is the most efficient way of doing this?

6条回答
  •  [愿得一人]
    2020-12-17 18:21

    Swift 5

    extension CGPoint {
        func rotate(around center: CGPoint, angle: CGFloat) -> CGPoint {
            let translate = CGAffineTransform(translationX: -center.x, y: -center.y)
            let transform = translate.concatenating(CGAffineTransform(rotationAngle: angle))
            let rotated = applying(transform)
            return rotated.applying(CGAffineTransform(translationX: center.x, y: center.y))
        }
    }
    

提交回复
热议问题