要开始一个定时器,可以使用NSTimer类的scheduledTimerWithTimeInterval方法。该方法会在指定的时间间隔过去后自动调用一个指定的方法。

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

上述代码创建了一个定时器,每隔1秒钟就会调用timerFired:方法,并且重复执行。

要结束一个定时器,可以使用invalidate方法。

[timer invalidate];

上述代码会停止并销毁之前创建的定时器。

注意:在使用定时器时,要确保在适当的时候停止和释放它,以避免内存泄漏和不必要的资源消耗。

ios 定时器开始和结束

原文地址: http://www.cveoy.top/t/topic/jbsl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录