如何使用MapKit快速旋转自定义userLocationAnnotationView图像? [英] How to rotate custom userLocationAnnotationView image using MapKit in swift?

查看:152
本文介绍了如何使用MapKit快速旋转自定义userLocationAnnotationView图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种使用MapKit在地图上显示用户方向的方法. MapKit的本机方法总是旋转整个地图. 由于用户位置也是MKAnnotationView,因此我决定创建一个特定的类来覆盖它并使用特定的图像(带有箭头).

I'm trying to find a way to show the direction of the user on the map using MapKit. The native MapKit way to do that always rotate the entire map. Since user position is also an MKAnnotationView, I decided to create a specific class to override it and use a specific image (with an arrow).

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函数中旋转该MKAnnotationView图像的方法.

Now I'm trying to find a way to rotate that MKAnnotationView image in the didUpdateHeading func of the locationManager.

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

var userLocationView :MKAnnotationView?

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    print(newHeading.magneticHeading)
}

newHeading.magneticHeading的打印有效并且非常准确. 现在如何旋转我的自定义UserLocationAnnotationView?

The print of newHeading.magneticHeading works and it pretty accurate. Now how can I rotate my custom UserLocationAnnotationView ?

感谢您的帮助.

推荐答案

目前我无法为您提供完整的代码示例,但希望我能为您提供一些指导.

I can't give you complete code examples at this point but I hope I can give you some pointers to go off on.

首先,我认为您不一定必须继承MKAnnotationView.您可以简单地将UIImage分配给其 image 属性.我认为,除非您不需要自定义,否则这将使事情变得更容易.

First, I don't think you would necessarily have to subclass MKAnnotationView. You could simply assign your UIImage to its image property. I think that would make things easier unless you do require the customization.

现在,我假设您已成功将注释添加到地图并具有对它的引用.

Now, I assume you have successfully added the annotation to the map and have a reference to it.

要旋转航向指示器,我看到三个选项:

To rotate the heading indicator, I see three options:

  1. 旋转MKAnnotationView image 财产:
    • 方法:标题更改时,创建UIImage的旋转副本并将其分配给image属性. 示例(未经测试).
    • 缺点:无法轻松为旋转设置动画.
  1. Rotate MKAnnotationView's image property:
    • Method: When the heading changes, create a rotated copy of the UIImage and assign it to the image property. Example (not tested).
    • Con: Can't easily animate the rotation.
  • 方法:更改标题时,请使用MKAnnotationView的/UIView CGAffineTransform .
  • 专业版:最简单
  • Con:同时旋转详细信息/标注视图.如果您需要这些,这将不是您的选择.
  • 方法:类似于2.但是使用 transform UIImageView上的属性直接不在MKAnnotationView本身上.这样便不会旋转标注视图.
  • 专业人士:应该运作良好.
  • 缺点:还有点工作.
  • Method: Similar to 2. but use the transform property on the UIImageView directly not on the MKAnnotationView itself. This way callout views are not rotated.
  • Pro: Should work well.
  • Con: A bit more work.

您还需要什么:

  • 将度数转换为弧度的功能.仿射变换需要弧度.
  • 如果要对旋转进行动画处理(1除外),请将更改包装到UIView static 查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆