2016-03-03 48 views
0

我在地图上有一个图钉,我想在经过一段时间后将其删除。我已经实施了所有适当的协议来放置注释。只是不知道该在哪里检查时间变量的值是否为一定量的>=如何使用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) 
    } 

回答

2

可以使用的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) 
} 
相关问题