如何使用 Swift 在经过一定时间后删除地图图钉?
How to make remove a map pin after a certain amount of time has passed using Swift?
我在地图上有一个图钉,我想在经过一定时间后将其移除。我已经实施了所有适当的协议来放置注释。只是不确定在哪里可以检查时间变量的值是否 >=
一定数量。
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)
}
我在地图上有一个图钉,我想在经过一定时间后将其移除。我已经实施了所有适当的协议来放置注释。只是不确定在哪里可以检查时间变量的值是否 >=
一定数量。
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)
}