2016-04-13 33 views
1

我想旋轉MKAnnotation圖像,雖然我成功這樣做,它的標題也旋轉。有沒有辦法讓標題保持直線?任何幫助將非常感激!旋轉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 
} 

類CustomPointAnnotation:MKPointAnnotation {

var imageName: String! 
var courseDegrees = 0.0 

}

enter image description here

回答

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 
}