2016-04-13 14 views
1

Ich versuche ein Bild für eine MKAnnotation zu drehen, und während ich das schaffe, dreht sich auch der Titel davon. Gibt es eine Möglichkeit, den Titel aufrecht zu erhalten? Jede Hilfe würde wirklich geschätzt werden!Rotate mkannotation image not title

Hier ist mein Code:

In 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 
} 

Klasse CustomPointAnnotation: MKPointAnnotation {

var imageName: String! 
var courseDegrees = 0.0 

}

enter image description here

Antwort

0

Ich habe es herausgefunden! Ich musste nur das Bild drehen, anstatt die gesamte Ansicht zu drehen.

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 
}