旋转 MKAnnotation 图像而不是标题

Rotate MKAnnotation image not title

我正在尝试旋转 MKAnnotation 的图像,虽然我成功了,但它的标题也在旋转。有没有办法保持标题直?任何帮助将非常感激!

这是我的代码:

viewDidLoad()中:

let middlePoint = CustomPointAnnotation()
middlePoint.coordinate = self.coordinates
middlePoint.imageName = "routemiddle"
middlePoint.title = "\(hourDetailedRoute):\(minuteDetailedRoute)"
middlePoint.courseDegrees = self.vehicleChangeCourse
self.mapa.addAnnotation(middlePoint)
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "annotation"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true
    }
    else {
        anView!.annotation = annotation
    }

    let cpa = annotation as! CustomPointAnnotation
    anView!.image = UIImage(named:cpa.imageName)
    anView!.transform = CGAffineTransformRotate(self.mapa.transform, CGFloat(degreesToRadians(cpa.courseDegrees)))

    return anView
}
class CustomPointAnnotation: MKPointAnnotation {
    var imageName: String!
    var courseDegrees = 0.0
}

我想通了!我只需要旋转图像而不是旋转整个视图。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "annotation"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true
    }
    else {
        anView!.annotation = annotation
    }

    let cpa = annotation as! CustomPointAnnotation
    var imagePin = UIImage(named:cpa.imageName)
    imagePin = imagePin?.rotateImage(cpa.courseDegrees)
    anView!.image = imagePin

    return anView
}