swift:定时器的使用、回调函数的指定方式

1.定时器:NSTimer类的函数,scheduledTimerWithTimeInterval()swift

根据字面意思:用时间间隔设置定时器。函数

例子(结合progressView制做加载动画):动画

 

另外,该函数(schedulerTimerwithTimeInterval)是会返回改定时器的对象的,因此回调函数的参数列表应该传入一个NSTimer对象如上。code

2. 指定回调函数的格式:对象

#selector(当前类名.回调函数名 或 self.回调函数名),不管是否有返回值给回调函数也是这种格式。如:回调函数

func progressAction(sender: NSTimer) {
    //回调函数内容
    //使定时器失效的方法:
    sender.invalidate()
}
相关文章
相关标签/搜索