在MapKit Swift3上旋转Pin方向

问题描述 投票:2回答:1

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

我希望在该Annotation上设置的图像以特定的程度旋转。

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

有人能帮助我吗?

enter image description here

ios swift annotations rotation mapkit
1个回答
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))

Update

如果您只想旋转图像,可以将图像放在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))
© www.soinside.com 2019 - 2024. All rights reserved.