自定义注释图像只在程序开始时旋转(Swift-iOS)

请帮助一个初学者的iOS开发者在这里! :)所以,我有一个计时器,定期从一个XML表提供公交车的实时位置的XML表获取公交车的经纬度。 我能够设置parsing器,为公交车运动设置animation并为公交车设置自定义(箭头)图像。

然而,问题是,每当我得到新的纬度和经度值时,我都无法旋转公交标注。 busAnnotation仅在程序启动busAnnotation旋转,但不是每次总线方向都更新。

下面是我的xml parser函数,它每次更改位置时都会计算busDirectionbusDirection的值随着位置变化而正确改变,但是由于某些原因,定时器继续运行,它不适用于旋转。 注释的移动也是非常生动的,除了程序启动之外,它不会旋转。

 // Some constants var oldLatitude: Double? = 44.97234 var oldLongitude: Double? = -93.2446329 var busDirection: CLLocationDirection() func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: NSDictionary!) { if (elementName == "vehicle") { var latitude = attributeDict["lat"]?.doubleValue // Get latitude of bus from xml var longitude = attributeDict["lon"]?.doubleValue // Get longitude of bus from xml if (oldLatitude == latitude && oldLongitude == longitude) { return } else { // Update bus location busDirection = directionBetweenPoints(MKMapPointForCoordinate(CLLocationCoordinate2DMake(oldLatitude!, oldLongitude!)), destinationPoint: MKMapPointForCoordinate(CLLocationCoordinate2DMake(latitude!, longitude!))) // Get direction of bus: UIView.animateWithDuration(0.5) { self.busAnnotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) self.mapView(self.map, viewForAnnotation: self.busAnnotation).annotation = self.busAnnotation self.map.addAnnotation(self.busAnnotation) } oldLatitude = latitude oldLongitude = longitude } } } 

这里是我为了改变注释的图像而实现的'viewForAnnotation',据说旋转它

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) pinView!.image = imageWithImage(UIImage(named:"arrow.png")!, scaledToSize: CGSize(width: 25.0, height: 25.0)) // Set custom image for annotation and resize it } else { pinView!.annotation = annotation } // Rotate the annotation. pinView?.transform = CGAffineTransformRotate(self.map.transform, CGFloat(degreesToRadians(self.busDirection))) return pinView } 

这些是我在上面的方法中使用的一些帮助函数:

 // For rotating bus annotations: func degreesToRadians(degrees: Double) -> Double { return degrees * M_PI / 180.0 } func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / M_PI } func directionBetweenPoints(sourcePoint: MKMapPoint, destinationPoint : MKMapPoint) -> CLLocationDirection { let x : Double = destinationPoint.x - sourcePoint.x; let y : Double = destinationPoint.y - sourcePoint.y; return fmod(radiansToDegrees(atan2(y, x)), 360.0) + 90.0; } 

你们认为我在这里错过了什么吗? 每次更新位置时,如何旋转公交注释? 提前致谢。

旋转仅在地图视图首次创build注释的视图时发生。 每次注释更新时,都不会创build新的视图,而只是移动视图。 这对于地图视图来说效率要高得多。 所以,你应该做的是使pinView属性,在你的init方法中实例化它,在mapView(, viewForAnnotation:)返回,并直接将旋转应用到你的animation块中的pinView

 UIView.animateWithDuration(0.5) { self.busAnnotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) self.map.addAnnotation(self.busAnnotation) if let pv = self.pinView { pv.transform = CGAffineTransformRotate(self.map.transform, CGFloat(degreesToRadians(self.busDirection))) } }