在Swift的同一个mapView中分配多个注释pinColors?

时间:2015-04-28 19:17:08

标签: ios iphone xcode swift annotations

所以我有一个地图视图,当用户在不同位置发布时添加了注释。我试图将引脚颜色更改为绿色,红色或紫色,具体取决于引脚创建后的长度。

我在这里遇到的问题是所有引脚都想要更改为一种指定颜色,而不是显示不同颜色的引脚。我似乎无法弄清楚这里出了什么问题。使用下面的示例代码将所有引脚设置为绿色,而实际上唯一应该为绿色的引脚是不到4小时前创建的引脚,红色引脚超过4小时但小于44小时,紫色引脚较旧超过44小时。

任何帮助表示赞赏!

pinColorQuery.findObjectsInBackgroundWithBlock {
            (dateCreated, error) -> Void in
            if error == nil {
                // The find succeeded.

                let createdDates = dateCreated as! [PFObject]

                for date in createdDates {
                    let createdAt = date.createdAt as NSDate!
                    let currentDate = NSDate()
                    let expirationDate = currentDate.timeIntervalSinceDate(date.createdAt!)

                    //This is where I attempt to set the pinColor based on age of pin
                    if (expirationDate < 18000) {
                        normalPinView!.pinColor = .Green
                    } else if (expirationDate < 162000) {
                        normalPinView!.pinColor = .Red
                    } else if (expirationDate > 162050) {
                        normalPinView!.pinColor = .Purple
                    }

                }

            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }

expirationDate间隔正确返回,我用简单的println("\(expirationDate)")检查了它。

1 个答案:

答案 0 :(得分:0)

我在mapview上使用过相同类型的功能,但是使用了objective-c。这不会直接改变已添加注释的引脚颜色。解决方法是以某种方式枚举所有注释并应用pincolor的逻辑,如果pincolor中有更改,则使用新的pincolor创建新注释但使用旧数据源然后删除旧注释并在同一位置添加新注释。就个人而言,它肯定会工作,这是实际的解决方案。在我的应用程序中,如果帖子的评论数超过10,50,我会更改引脚颜色和引脚大小等等。

Objective-c代码:在某些功能中。

anothersite.com

这只是一个粗略的想法,会有拼写错误,它不是快速的,而是循环,你可以使用枚举。