NSTime


    //第一种,自动触发定时器
    {
        NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(t:) userInfo:@{@"sdfsdf":@"df"} repeats:YES];
        
        NSTimer *time2 = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(t:) userInfo:@"sdfsdf" repeats:YES];

        //这里是提前触发,不写也会在设定间隔时间触发
        [time fire];
    
    //对应的销毁函数
//    [time invalidate];
    }
    
    
    //第二种,手动触发
    {
        NSTimer *time3 = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(t:) userInfo:@"sdf" repeats:YES];

        NSRunLoop *runloop=[NSRunLoop currentRunLoop];
        [runloop addTimer:time3 forMode:NSDefaultRunLoopMode];
    }
    
    
    //第三种
    {
        //初始化一个Invocation对象
        NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];
        [invo setTarget:self];
        [invo setSelector:@selector(t:)];
        NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
        //加入主循环池中
        [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
        //开始循环
        [timer fire];
        
    }
}

-(void)t:(NSTimer*)n
{
    //传参数  NSNumber  字典  字符串
    id  t = [n userInfo];
    
    //触发的系统时间
    NSData *d = n.fireDate;

}


//暂停、启动、销毁

if(btn.tag == k_link)
    {
        [heartTimer setFireDate:[NSDate distantFuture]];  //暂停
    }
    else if (btn.tag == k_link+1)
    {
        [heartTimer setFireDate:[NSDate distantPast]];   //启动
    }
    else
    {
        [heartTimer invalidate];  //销毁
        heartTimer = nil;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值