2016-04-13 21 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 
}