BBann
BBann

Reputation: 33

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?

UPDATE:

Everything compiling OK, but when i run my app in simulator(i do not have any iOS devices), my imageview not rotate in true north.

Why?

import UIKit
import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {

    var manager: CLLocationManager?
    var imageView: UIImageView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CLLocationManager()
        manager?.delegate = self;
        manager?.desiredAccuracy = kCLLocationAccuracyBest


        imageView = UIImageView(image:#imageLiteral(resourceName: "pointer"))
        self.view.addSubview(imageView!)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        imageView?.transform = CGAffineTransform(rotationAngle: CGFloat(newHeading.trueHeading))

    }

}

Upvotes: 2

Views: 719

Answers (1)

Unheilig
Unheilig

Reputation: 16292

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.
}

Upvotes: 4

Related Questions