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

后端 未结 6 1725
轻奢々
轻奢々 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:14

    Using Vladimir’s answer, below is the Swift 3 answer:

        let initialPoint = CGPoint(x: 100, y: 100) // the point you want to rotate
        let translateTransform = CGAffineTransform(translationX: initialPoint.x, y: initialPoint.y)
        let rotationTransform = CGAffineTransform(rotationAngle: angle)
        let customRotation = (rotationTransform.concatenating(translateTransform.inverted())).concatenating(translateTransform)
        rotatedPoint = initialPoint.applying(customRotation)
    

提交回复
热议问题