随机数
一个小小的随机数工具。。。。。。。
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 }