BBann BBann - 1 month ago 17
iOS Question

How to convert type double to CGAffineTransform?

When I rotate my image view:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
imageView?.transform = newHeading.trueHeading

}


I get the following error:


Cannot assign value of type 'CLLocationDirection' (aka 'Double') to type 'CGAffineTransform'


How to convert/assign it?

Answer

trueHeading is of type Double in degrees.

In order to rotate the UIImageView, one has to provide a transform:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
    imageView?.transform = CGAffineTransform(rotationAngle: CGFloat(newHeading.trueHeading  * .pi / 180))
    // converting the value from degrees to radians, which CGAffineTransform expects.
}