自定义注释图像仅在程序开始时旋转(Swift-iOS) [英] Custom annotation image rotates only at the beginning of the Program (Swift- iOS)

查看:22
本文介绍了自定义注释图像仅在程序开始时旋转(Swift-iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在这里帮助初学者 iOS 开发者!:) 所以,我有一个计时器,它定期从提供总线实时位置的 xml 表中获取总线的纬度和经度.我能够设置解析器、为总线运动设置动画并为总线设置自定义(箭头)图像.

Please help out a beginner iOS developer here! :) So, I have a timer which gets the latitude and longitude of a bus periodically from a xml sheet which provides real-time locations of the bus. I was able to setup the parser, animate the bus movement and setup a custom (arrow) image for the bus.

然而,问题是,每次我获得新的纬度和经度值时,我都无法旋转总线注释.busAnnotation 仅在程序启动时旋转,而不是每次更新总线方向时旋转.

However, the problem is, I'm not able to able to rotate the bus-annotation everytime I get new values for the latitude and longitude. The busAnnotation rotates only when the program starts, but not everytime the direction of the bus is updated.

下面是我的xml parser 函数,它会在每次位置改变时计算busDirection.busDirection 的值会随着位置的变化而正确地变化,但由于某种原因,它不适用于定时器的旋转.注释的移动也有很好的动画效果,只是在程序启动时不旋转.

Below is my xml parser function, which calculates the busDirection everytime the location changes. The value of busDirection changes correctly with location changes, but it doesn't apply to the rotation for some reason as the timer goes on. The movement of the annotation is also well-animated, it just doesn't rotate other than when the program is started.

// 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",目的是改变注释的图像并旋转它

Here is the 'viewForAnnotation' that I implemented in order to change the image of the annotation and supposedly rotate it

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
}

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

These are some of the helper functions that I used in the above methods:

// 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;
}

你们认为我在这里遗漏了什么吗?每次更新位置时,我将如何旋转总线注释?提前致谢.

Do you guys think I'm missing something here? How would I rotate the bus-annotation everytime the locations are updated? Thanks in advance.

推荐答案

旋转仅在地图视图第一次为注释创建视图时发生.不是每次注释更新时都创建一个新视图,它只是移动视图.这对于地图视图来说效率更高.所以,你应该做的是使 pinView 成为一个属性,在你的 init 方法中实例化它,在 mapView(, viewForAnnotation:) 中返回它并直接应用旋转pinView 在你的动画块中.

The rotation only occurs the first time the map view creates the view for the annotation. Rather than create a new view every time an annotation updates, it simply moves the view. That's much more efficient for the map view to do. So, what you should do is make the pinView a property, instantiate it in your init method, return that in mapView(, viewForAnnotation:) and apply the rotation directly to that pinView in your animation block.

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

这篇关于自定义注释图像仅在程序开始时旋转(Swift-iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆