在MapKit Swift3上旋转Pin方向

时间:2017-12-28 00:31:04

标签: ios swift annotations rotation mapkit

我正在尝试旋转设置为放置在MapKit上的Annotation的UIImage。

我希望在该注释上设置的图像以特定的角度旋转。

我已经尝试了一些方法,但它们没有用。

有人能帮助我吗?

enter image description here

This is what I tried to do, but didn't work

1 个答案:

答案 0 :(得分:3)

你应该替换

annotationView.transform.rotated(by:CGFloat(M_PI_4))

由此:

annotationView.transform = CGAffineTransform(rotationAngle:CGFloat(Double.pi/4.0))

或:

annotationView.transform = annotationView.transform.rotated(by: CGFloat(Double.pi/4.0))

更新

如果您只想旋转图像,可以将图像放在UIImageView中,并将UIImageView作为子视图添加到MKAnnotationView中。下面是一个关于如何操作的简单示例(不要直接使用,它不完全)。

class CustomMKAnnotationView: MKAnnotationView {
    var imageView: UIImageView?

    override func prepareForReuse() {
        imageView?.image = nil
    }

    func updateImge(image: UIImage?) {
        guard let aImage = image else {
            return
        }

        if imageView == nil {
            imageView = UIImageView(image: aImage)
            if imageView != nil {
                frame = imageView!.frame
                addSubview(imageView!)
            }
        } else {
            imageView!.image = aImage
            frame = imageView!.frame
        }
    }
}

mapView:viewForAnnotation:

annotationView.updateImge(image: UIImage(named: "car.png"))
annotationView.imageView?.transform = CGAffineTransform(rotationAngle:CGFloat(Double.pi/4.0))