热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

swiftMapKit注释拖拽状态图标-swiftMapKitannotationdragstateicon

Ihaverunintoaslightproblem.IamtryingtouseacustomiconformymapViewannotation.Thetr

I have run into a slight problem. I am trying to use a custom icon for my mapView annotation. The trouble is that when the user drags the icon it always changes back to the default icon.

我遇到了一个小问题。我正在尝试为我的mapView注释使用一个自定义图标。问题是当用户拖拽图标时,它总是会切换回默认图标。

I set the icon image in my mapView delegate like so, this works to set the icon.

我在mapView委托中设置图标图像,就像这样,这是用来设置图标的。

// MARK: - Map Annotations
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation{
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if(pinView == nil){
        if let customAnnot = annotation as? myAnnotation {
            pinView = MKPinAnnotationView(annotation: customAnnot, reuseIdentifier: reuseId)


            pinView!.image = UIImage(named:"pin-50.png")


            pinView!.animatesDrop = false
            pinView!.draggable = true
        }

    } else {
        pinView!.annotation = annotation as? myAnnotation
    }

    return pinView!
}

I tried a few things to fix but none have seem to helped. even when I try to set the icon again in the "didChangeDragState" delegate it still changes to default icon.

我试了几样东西去修复,但似乎没有一件对我有帮助。即使我尝试在“didChangeDragState”委托中再次设置图标,它仍然会更改为默认图标。

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.Dragging {
        println("draggin it")
        view.image = UIImage(named:"pin-50.png")
    }

    if newState == MKAnnotationViewDragState.Ending {
        //update pin location
        if let customAnnot = view.annotation as? myAnnotation {
            cData.updatePinLocation(customAnnot.pinID, newValue: customAnnot.coordinate)
        }
        view.image = UIImage(named:"pin-50.png")

    }

    if newState == MKAnnotationViewDragState.Starting {
        println("start drag")
        view.image = UIImage(named:"pin-50.png")
    }

}

1 个解决方案

#1


2  

Thanks to zisoft, I figured it out. here is the code that works

多亏了zisoft,我找到了答案。这是有效的代码

   if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.image = UIImage(named:"pin-50.png")
        pinView.canShowCallout = false
        pinView.draggable = true
    }
    else {

        pinView.annotation = annotation
    }

    return pinView

推荐阅读
author-avatar
松狮猫vn
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有