2016-10-30 129 views
2

当我旋转我的形象的看法:如何将double类型转换为CGAffineTransform?

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

} 

我得到以下错误:

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

如何转换/分配呢?

UPDATE:

一切编译好了,但是当我在模拟器中运行我的应用程序(我没有任何iOS设备),我的ImageView的不是真北旋转。

为什么?

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)) 

    } 

} 
+0

什么是你想在这里做什么?你想旋转图像视图吗? – rmaddy

+0

是@rmaddy我想旋转我的图像视图当我旋转设备 – BBann

回答

3

trueHeading是以度Double类型。

为了旋转UIImageView,一个必须提供的变换:

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

嘿@Unhellig检查我更新的问题,请 – BBann