iOS开发之 简易随机点名册的设计

来源:互联网 发布:美国网络空军 编辑:程序博客网 时间:2024/06/07 06:21

效果图如下:



先看看定义的全局变量:

<span style="font-size:14px;color:#666666;">{    NSMutableArray *arrName;// 后台所用名字的数组    UILabel *label ;// 显示抽到的名字的label    NSTimer *timer;// 在点击开始按钮后,定时器开始运行    UIButton *button;// 开始的按钮}</span>


具体代码如下:

<span style="font-size:18px;">    </span><span style="font-size:14px;">// 后台所用名字的数组    arrName = [NSMutableArray arrayWithObjects:@"刘亭均",@"黄方果",@"杨鹏飞",@"周总", nil];        // 开始的按钮    self.view.backgroundColor = [UIColor grayColor];    button = [UIButton buttonWithType:UIButtonTypeCustom];    button.backgroundColor = [UIColor greenColor];    button.frame = CGRectMake(120, 500, 100, 35);    [button setTitle:@"开始" forState:UIControlStateNormal];    button.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2+100);     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];    [self.view addSubview:button];    [button addTarget:self action:@selector(find) forControlEvents:UIControlEventTouchUpInside];        // 在点击开始按钮后,定时器开始运行    timer = [NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];    timer.fireDate = [NSDate distantFuture ];            // 显示抽到的名字的label    label = [[UILabel alloc]init];    label.backgroundColor = [UIColor whiteColor];    label.frame = CGRectMake(0, 0, 150, 35);    label.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2-100);    label.text = @"猜猜点到谁";    label.textColor = [UIColor blackColor];    label.textAlignment = NSTextAlignmentCenter;    [self.view addSubview:label];}#pragma mark  -----------设置button在点击的时候开始或者暂停-------------- (void)find{    if (button.selected != YES) {        button.selected = YES;         [button setTitle:@"暂停" forState:UIControlStateNormal];        [self start];    }else{        button.selected = NO;        [button setTitle:@"开始" forState:UIControlStateNormal];        [self stop];    }}#pragma mark  ---------点击开始按钮后,展示的label框展示随机晃动的名字------------- (void)zhuan{        int i = arc4random()%arrName.count;        label.text = [NSString stringWithFormat:@"中奖者是:%@",arrName[i]];}- (void)start{    timer.fireDate = [NSDate distantPast];}- (void)stop{    timer.fireDate = [NSDate distantFuture];    NSString *s =  label.text;    if(arrName.count >1) {        [arrName removeObject:s];    }    for (NSString *name in arrName) {                NSLog(@"%@",name);    }}</span>

我们所用得名字数组是可变的,所以说我们只要改变我们自己的名字数组,就成了一个简单的点名器了。



0 0
原创粉丝点击