答案 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))