代码不多就不提供demo了,直接贴代码,供娱乐!
//
// ViewController.m
// 摇号
//
// Created by 纪高飞 on 17/5/13.
// Copyright © 2017年 纪高飞. All rights reserved.
//
#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
/**lab*/
@property (nonatomic,strong) UILabel *textlab ;
/**数据源*/
@property (nonatomic,strong) NSMutableArray * dataArray;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation ViewController
#pragma mark - 懒加载
- (NSMutableArray *)dataArray
{
if (!_dataArray) {
self.dataArray = [[NSMutableArray alloc]init];
}
return _dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.dataArray = @[@"张杰",@刘亦菲",@"赵丽颖",@"刘若英",@"张惠妹",@"范冰冰冰"].mutableCopy;
UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
startBtn.frame = CGRectMake(50, 100, kScreenW -100 ,30);
startBtn.backgroundColor = [UIColor redColor];
[startBtn setTitle:@"开始" forState:UIControlStateNormal];
[startBtn addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startBtn];
UIButton *stopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
stopBtn.frame = CGRectMake(50, 200, kScreenW -100 ,30);
stopBtn.backgroundColor = [UIColor orangeColor];
[stopBtn setTitle:@"结束" forState:UIControlStateNormal];
[stopBtn addTarget:self action:@selector(stopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopBtn];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 260, 100 ,30)];
title.text = @"今日中奖:";
[self.view addSubview:title];
self.textlab.textAlignment = NSTextAlignmentCenter;
_textlab.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textlab];
self.textlab = [[UILabel alloc]initWithFrame:CGRectMake(120, 260, kScreenW -240 ,30)];
self.textlab.textColor = [UIColor redColor];
self.textlab.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_textlab];
}
#pragma mark 点击事件
- (void)startAction:(UIButton *)sender
{
[_timer invalidate];
NSLog(@"开始");
// 创建定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)stopBtnAction:(UIButton *)sender
{
NSLog(@"结束");
[_timer invalidate];
}
- (void)timerAction{
int y = 0 + arc4random()%(100 - 0 + 1);
self.textlab.text =[NSString stringWithFormat:@"%d",y];
// self.dataArray = @[@"张杰",@刘亦菲",@"赵丽颖",@"刘若英",@"张惠妹",@"范冰冰"].mutableCopy;
// self.textlab.text = self.dataArray[y];
}
@end