How to rotate custom userLocationAnnotationView image using MapKit in swift?

好久不见. 提交于 2019-11-30 23:10:30

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

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. 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.
  2. Rotate the MKAnnotationView itself:
    • Method: When the heading changes, use MKAnnotationView's/UIView's transform property. Assign an appropriate CGAffineTransform to it.
    • Pro: Easiest
    • Con: Also rotates the detail/callout views. If you need these, this will not be an option for you.
  3. Put the UIImage into a UIImageView and add that one as a subview to 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.

What you also need:

  • A function to convert from degrees to radians. The affine transformations require radians.
  • If you want to animate the rotation (except for 1.) wrap the change to the transform property in UIView's static animate method.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!