ios - 如何使用 Swift 在经过一定时间后删除 map 图钉?

标签 ios swift mkmapview mapkit mkpointannotation

我想在经过一定时间后将 map 上的图钉移除。我已经实现了所有适当的协议(protocol)来放置注释。只是不确定在哪里可以检查时间变量的值是否 >= 一定数量。

var timer = NSTimer()

let annotation = MKPointAnnotation()

var time = 0

//按钮按下创建一个图钉

@IBAction func buttonPressed(sender: AnyObject) {

        func timerFunc() {

            time++

        }

        annotation.coordinate = location

        annotation.title = "Place"

        annotation.subtitle = "Description"

        map.addAnnotation(annotation)

            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerFunc"), userInfo: nil, repeats: true)

        }

        if time >= 5 {
            timer.invalidate()
        } 

    }

现在看来我需要在某处放置这样的东西:

if time >= 5 {
    map.removeAnnotation(annotation)
  }

最佳答案

您可以使用 NSTimer 调用一个方法来删除注释。

var myTimer: NSTimer!


@IBAction func buttonPressed(sender: AnyObject) {

    annotation.coordinate = location
    annotation.title = "Place"
    annotation.subtitle = "Description"
    map.addAnnotation(annotation)
    myTimer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "runTimedCode", userInfo: nil, repeats: false)
    }
}

//this method will be called after 5 seconds
func runTimedCode() {  
       map.removeAnnotation(annotation)
}

关于ios - 如何使用 Swift 在经过一定时间后删除 map 图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764366/

相关文章:

ios - 将 cocoapods 添加到使用 cordova 6.5 创建的 iOS 工作区

ios - 错误域=com.flickr 代码=108 "The operation couldn’ t 完成

ios - 为什么 ABCreateStringWithAddressDictionary 没有给出详细名称?

ios - 从 calloutAccessoryControlTapped 设置 ViewController Title

ios - MKMapView on Storyboard on multiple Views with just 1 instance

将 Angular 项目从版本 6 更新到版本 8 后,IOS Scroll 无法正常工作?

ios - 在 UITextview 中复制 NSDictionary 值

ios - 如何使用泛型来处理字典和数组

ios - 如何运行命令

ios - 在 UI ImageView 中检索和显示保存的图像(文档文件夹)