如何使用MapKit快速旋转自定义userLocationAnnotationView图像? [英] How to rotate custom userLocationAnnotationView image using MapKit in swift?
问题描述
我正在尝试找到一种使用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:
- Rotate
MKAnnotationView
'simage
property:- Method: When the heading changes, create a rotated copy of the
UIImage
and assign it to theimage
property. Example (not tested). - Con: Can't easily animate the rotation.
- Method: When the heading changes, create a rotated copy of the
- 方法:更改标题时,请使用
MKAnnotationView
的/UIView
的CGAffineTransform
. - 专业版:最简单
- Con:同时旋转详细信息/标注视图.如果您需要这些,这将不是您的选择.
- 方法:类似于2.但是使用
transform
UIImageView
上的属性直接不在MKAnnotationView
本身上.这样便不会旋转标注视图. - 专业人士:应该运作良好.
- 缺点:还有点工作.
- Method: Similar to 2. but use the
transform
property on theUIImageView
directly not on theMKAnnotationView
itself. This way callout views are not rotated. - Pro: Should work well.
- Con: A bit more work.
您还需要什么:
- 将度数转换为弧度的功能.仿射变换需要弧度.
- 如果要对旋转进行动画处理(1除外),请将更改包装到
UIView
的 static 查看全文