一:NSTimer

当时间间隔>1s是用NSTimer;

方法:

[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

关于内存释放

  如果我们启动了一个定时器,在某个界面释放前,将这个定时器停止,甚至置为nil,都不能是这个界面释放,原因是系统的循环池中还保有这个对象。所以我们需要这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-(void)dealloc{
    NSLog(@"dealloc:%@",[self class]);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myLog:) userInfo:nil repeats:YES];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    btn.backgroundColor=[UIColor redColor];
    [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)btn{
    if (timer.isValid) {
        [timer invalidate];
    }
    timer=nil;
    [self dismissViewControllerAnimated:YES completion:nil];
}

在官方文档中我们可以看到 [timer invalidate]   timer=nil;是唯一的方法将定时器从循环池中移除。

雪花定时器

- (void)awakeFromNib

{

// 添加定时器 0.1  0.35

//    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

// 一般如果定时器调用频率非常高,<1s,通常不会使用NSTimer,NSTimer调用优先级不高,NSTimer在重绘的时候也不要使用

CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];

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

}

//- (void)timeChange

//{

//    // setNeedsDisplay 这个方法并不会马上调用 drawRect方法,仅仅是给当前视图做一个重绘标记,当下一次屏幕刷新的时候就会调用drawRect方法

//    [self setNeedsDisplay];

//}

static int _snowY = 0;

- (void)drawRect:(CGRect)rect {

// Drawing code

   UIImage *image = [UIImage imageNamed:@"雪花"];

[image drawAtPoint:CGPointMake(50, _snowY)];

_snowY += 10;

if (_snowY > rect.size.height) {

_snowY = 0;

}

}

二:时间段.--计算一段代码的效率

1s = 1000ms = 10^3ms(毫秒) = 10^6μs (微秒) = 10^9ns (纳秒) = 10^12ps (皮秒) = 10^15fs (飞秒)

    //当前时间:
double date_s = CFAbsoluteTimeGetCurrent();for (int i = ; i <; i++) {
NSLog(@"I Love You!!!");
}
//时间差.
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@" ForLoop Time: %f ms",date_current * );

  

最新文章

  1. HTTPS那些事(一)HTTPS原理
  2. ASP.NET MVC 利用IRouteHandler, IHttpHandler实现图片防盗链
  3. AC日记——地鼠游戏 codevs 1052
  4. WinForm------GridControl显示每行的Indicator中的行号
  5. poj1328解题报告(贪心、线段交集)
  6. BindingFlags说明
  7. 文档学习 - UILabel - 属性详解
  8. [Angular + Webpack] ocLazyLoad compoment
  9. cf C. Vasya and Robot
  10. PropertiesDemo
  11. Kotlin 初窥门径[2]:流程控制
  12. POJ 3928 &amp;amp; hdu 2492 &amp;amp; Uva1428 PingPong 【树状数组】
  13. idea创建springboot Web项目
  14. ivew ui
  15. Go学习笔记(五)Go命令工具
  16. Python字符串拼接的6种方法(转)
  17. ssh采用expect实现自动输入密码登录、拷贝
  18. Python练习之用户登录-5
  19. 廖雪峰Java1-4数组操作-1遍历数组
  20. IntelliJ IDEA 2017版 spring-boot 2.03后 Pageable用法;Pageable用法,PageRequest过时,新用法;Pageable过时问题;

热门文章

  1. Ajax &amp;&amp; json(原始)
  2. Struts2 与SpringMVC之比较
  3. (转) go Cron的使用
  4. 用 jQuery实现图片等比例缩放大小
  5. Go -- 一致性哈希算法
  6. ElasticSearch生产模式开发模式的区分
  7. C++简单实现对象引用计数示例(转)
  8. Go fsm
  9. IO模型:同步、异步、阻塞、非阻塞
  10. [LightOJ 1018]Brush (IV)[状压DP]