1
我试图找到一种方法来显示使用MapKit的用户在地图上的方向。 原生的MapKit方式总是会旋转整个地图。 由于用户位置也是一个MKAnnotationView,我决定创建一个特定的类来覆盖它并使用特定的图像(带有箭头)。如何在swift中使用MapKit自定义userLocationAnnotationView图像?
class UserLocationAnnotationView: MKAnnotationView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(annotation: MKAnnotation!, reuseIdentifier: String!) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
var frame = self.frame
frame.size = CGSizeMake(130, 130)
self.frame = frame
self.backgroundColor = UIColor.clearColor()
self.centerOffset = CGPointMake(-30, -50)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
override func drawRect(rect: CGRect) {
// Drawing code
UIImage(named: "userLocation.png")?.drawInRect(CGRectMake(65, 65, 65, 65))
}
现在我试图找到一种方法在的LocationManager的didUpdateHeading FUNC旋转该MKAnnotationView图像。
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var userLocationView :MKAnnotationView?
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print(newHeading.magneticHeading)
}
newHeading.magneticHeading的版画作品,它相当准确。 现在如何旋转我的自定义UserLocationAnnotationView?
感谢您的帮助。
非常感谢@Arthur!即使你没有给出任何代码,你的答案是确切的,并用你的不同方法,我发现我的方式使其工作。我决定使用第二种方法,因为我不需要userAnnotationView的任何标注视图。另外最后一条关于使用UIView动画方法的建议非常有帮助。再次感谢你 – matthioo