你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS-隨機點名或抽獎游戲的實現

iOS-隨機點名或抽獎游戲的實現

編輯:IOS開發綜合

 

 

#import ViewController.h

 

@interface ViewController ()

{

NSMutableArray *nameList;

UILabel *label;

NSTimer *timer;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

[super viewDidLoad];

[self loadData];

 

 

self.view.backgroundColor = [UIColor grayColor];

label = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 275, 50)];

label.text = @不會是我吧;

 

label.textAlignment = NSTextAlignmentCenter;

label.font = [UIFont systemFontOfSize:30];

label.textColor = [UIColor orangeColor];

label.backgroundColor = [UIColor clearColor];

[self.view addSubview:label];

 

 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(150, 500, 100, 100);

[self.view addSubview:button];

[button setTitle:@點我啊 forState:UIControlStateNormal];

button.backgroundColor = [UIColor redColor];

button.layer.cornerRadius = 50;

button.layer.masksToBounds = YES;

button.showsTouchWhenHighlighted = YES;

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

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

 

timer = [NSTimer scheduledTimerWithTimeInterval:1 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:@a,@b,@c,@d,@e,@f,@g俊,@f,@gd,@fv,@sd,@f,@sdf,@4t,@i紅,@k,@u,@i海,@ijk,@k,@i林,@楊小平,@i均,@ui飛,@i寬富,@i旺,@e浩,@i誠,@i文興,@i水,@i波,@tu金奎,@i,@王e,@5j來,@l康,@i8黔,@k.意,@;龍,@u波,@ol忠6華,@y濤, nil];

[userDefaults setObject:nameList forKey:@nameList];

[userDefaults synchronize];

[userDefaults objectForKey:@nameList];

int arcNumber = arc4random()%nameList.count;

label.text = nameList[arcNumber];

 

 

}

 

- (void)push:(UIButton *)sender

{

if (sender.selected !=YES ) {

label.textColor = [UIColor clearColor];

[sender setTitle:@暫停 forState:UIControlStateNormal];

// [nameList removeObject:label.text];

timer.fireDate = [NSDate distantPast];

sender.selected = YES;

}else{

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];

 

}

 

 

@end


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved