zoukankan      html  css  js  c++  java
  • 随机数

        一个小小的随机数工具。。。。。。。

     1 self.view.backgroundColor = [UIColor yellowColor];
     2     
     3     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 240, 60)];
     4     label.font = [UIFont boldSystemFontOfSize:30];
     5     label.text = @"点击开始随机";
     6     label.backgroundColor = [UIColor grayColor];
     7     label.textAlignment = NSTextAlignmentCenter;
     8     label.tag = 1;
     9     [self.view addSubview:label];
    10     [label release];
    11     
    12     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    13     btn.frame = CGRectMake(120, 180, 80, 40);
    14     btn.backgroundColor = [UIColor grayColor];
    15     [btn setTitle:@"开始" forState:UIControlStateNormal];
    16     [btn setTitle:@"停止" forState:UIControlStateSelected];
    17     [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    18     [self.view addSubview:btn];
    19 }
    20 
    21 - (void)btnClick:(UIButton *)sender
    22 {
    23     sender.selected = !sender.selected;
    24     
    25     if (sender.selected) {
    26         
    27         _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
    28         //启动时间器,让self每隔0.01秒执行一次timeRun
    29         
    30     } else {
    31         
    32         [_timer invalidate];
    33         //关闭时间器
    34     }
    35 }
    36 
    37 - (void)timeRun
    38 {
    39     int row = arc4random_uniform(8)+1;
    40     int num = arc4random_uniform(11)+1;
    41     
    42     UILabel *label = (UILabel *)[self.view viewWithTag:1];
    43     //通过tag找view
    44     
    45     label.text = [NSString stringWithFormat:@"%d%02d",row,num];
    46 }
  • 相关阅读:
    03-Spring默认标签解析
    想要写出好味道的代码,你需要养成这些好习惯!
    IDEA 缺少Springboot启动图标 如何添加
    echarts的canvas大小
    JS控制div上下滚动内容
    2020新的一年开始了
    2019年第一个工作日!
    关于.net项目前后端分离框架(一)
    MongoDB学习一:安装及简单使用
    spring默认标签与自定义标签学习
  • 原文地址:https://www.cnblogs.com/Angelone/p/4384817.html
Copyright © 2011-2022 走看看