NSTimer

作者: 那位小姐 | 来源:发表于2018-03-27 14:17 被阅读18次

创建一个 Timer

/*
* 预订一个Timer,设置一个时间间隔。
  表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1

 target:(id)aTarget
表示发送的对象,如self

 selector:(SEL)aSelector
方法选择器,在时间间隔内,选择调用一个实例方法

userInfo:(id)userInfo
此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。

repeats:(BOOL)yesOrNo
当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。

invocation:(NSInvocation *)invocation

*
*/
+ scheduledTimerWithTimeInterval: invocation: repeats:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   invocation:(NSInvocation *)invocation   repeats:(BOOL)yesOrNo;
+ scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   target:(id)aTarget   selector:(SEL)aSelector   userInfo:(id)userInfo   repeats:(BOOL)yesOrNo;
创建返回一个新的NSTimer对象和时间表,在当前的默认模式下循环调用一个实例方法。
+ timerWithTimeInterval: invocation: repeats:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ timerWithTimeInterval: target:selector: userInfo:repeats:
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
– initWithFireDate: interval: target: selector: userInfo: repeats:
- (id)initWithFireDate:(NSDate *)date   interval:(NSTimeInterval)ti     target:(id)t    selector:(SEL)s    userInfo:(id)ui    repeats:(BOOL)rep;

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

启动 Timer

– fire
//加定时器

[[NSRunLoop currentRunLoop]run];

停止 Timer

– invalidate

Timer设置

– isValid
– fireDate
– setFireDate:
– timeInterval
– userInfo

NSTimeInterval类:是一个浮点数字,用来定义秒

例子:
iphone为我们提供了一个很强大得时间定时器 NSTimer

他可以完成任何定时功能:

我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理

delegate和事件处理方法@selector();就可以用

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 来初始化一个 时间定时器

绘画中很少用NSTimer 另一个定时器

// 每次屏幕刷新的时候就会触发,通常我们的屏幕一秒刷新60次,
    // 1秒调用60次
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];

    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

获取当前时分秒

   // 创建日期类
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 日期组件 (年、月、日、小时、分、秒)
    /**
     *  NSCalendarUnitEra
        NSCalendarUnitYear
        NSCalendarUnitMonth
        NSCalendarUnitDay
        NSCalendarUnitHour
        NSCalendarUnitMinute
        NSCalendarUnitSecond
        NSCalendarUnitWeekday
        NSCalendarUnitWeekdayOrdinal
        NSCalendarUnitQuarter
        NSCalendarUnitWeekOfMonth
     */
    NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
    //获得当前秒
    NSInteger sec = cmp.second;
    //获得当前分钟
    NSInteger min = cmp.minute;
    //获得当前小时
    NSInteger hour = cmp.hour;

相关文章

网友评论

      本文标题:NSTimer

      本文链接:https://www.haomeiwen.com/subject/xitscftx.html