闻道丶技能(iOS)1 iOS Developer

轻松搞定随机抽击,随机点名

2017-08-30  本文已影响235人  曼纪珂

创建VC,在VC的viewdidload中创建显示Label,和触发按钮,具体实现步骤如下:

- (void)viewDidLoad {

[super viewDidLoad];

[self loadData];

self.view.backgroundColor=[UIColor whiteColor];

label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 60)];

label.text = @"七夕送什么礼物";

label.textAlignment = NSTextAlignmentCenter;

label.font = [UIFont boldSystemFontOfSize:50];

label.textColor = [UIColor orangeColor];

label.backgroundColor = [UIColor clearColor];

[self.view addSubview:label];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake((self.view.frame.size.width-100)/2, 500, 100, 40);

[self.view addSubview:button];

[button setTitle:@"开始选" forState:UIControlStateNormal];

button.backgroundColor = UIColorFromRGB(0x4fba33);

button.showsTouchWhenHighlighted = YES;

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[button addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];

timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(loadData) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop ]addTimer:timer forMode:NSDefaultRunLoopMode];

timer.fireDate = [NSDate distantFuture];

}

- (void)loadData

{

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

nameList = [NSMutableArray arrayWithObjects:@"包包",@"nike",@"阿迪达斯",@"衣服",@"化妆品",@"香水",@"补水霜",@"香薰",@"绣眉笔",@"项链",@"手表",@"手环",@"项链",@"手镯",@"戒指",@"爱疯7普拉斯",@"macPro",@"MK",@"路易威登",@"车",@"房",@"啥都不要",@"1000块",@"100块",@"大吃一顿",@"出去玩一趟",@"鲜花",@"ipad",@"游戏机",@"挂件",@"装饰品",@"背包",@"单肩包",@"裙子",@"帽子",@"扎花",@"拖鞋",@"睡衣",@"美食",@"小摩托",@"自行车",@"滑板车", nil];

[userDefaults setObject:nameList forKey:@"nameList"];

[userDefaults synchronize];

[userDefaults objectForKey:@"nameList"];

int arcNumber = arc4random()%nameList.count;

label.text = nameList[arcNumber];

label.textColor= UIColorFromRGB(0x4fba33);

}

- (void)push:(UIButton *)sender{

if (sender.selected !=YES ) {

label.textColor = [UIColor clearColor];

[sender setTitle:@"暂停" forState:UIControlStateNormal];

timer.fireDate = [NSDate distantPast];

sender.selected = YES;

}else{

[SVProgressHUD showInfoWithStatus:@"好了,那就这个吧"];

timer.fireDate = [NSDate distantFuture];

label.textColor = [UIColor blackColor];

[sender setTitle:@"开始选" forState:UIControlStateNormal];

sender.selected = NO;

}

}

- (void)showAlertMessage:(NSString *)message

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"真的是你" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];

}

以上就是适用于随机点名,随机抽奖功能,个人意见,不喜勿喷,交流心得。

附上github地址:https://github.com/zhaoMrPlus/Randomdraw

上一篇 下一篇

猜你喜欢

热点阅读