您的位置:首页 > 移动开发 > IOS开发

iOS 简单的猜颜色游戏 界面设计的比较简单 - -

2015-07-31 13:54 363 查看
@interface ViewController ()
{
UILabel *jifen;
UILabel *label;
UIButton *button;
UIButton *button1;
NSTimer *timer;
NSTimer *timer1;
NSArray *color;
int showTime;
UILabel *NO1;
UILabel *show;
NSArray *anniu;
int b;
int index;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 开始按钮相关
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(150, 647, 80, 40);
button.center = CGPointMake(187.5, 627);
button.backgroundColor = [UIColor darkTextColor];
button.alpha = 0.6;
[button setTitle:@"开始" forState:UIControlStateNormal];
button.layer.cornerRadius = 4;
button.layer.masksToBounds = YES;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

// 颜色按钮
anniu = @[@"橘红色",@"黑色",@"蓝色"];
for (int i = 0; i < 3; i ++) {
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame =CGRectMake(61.5+(80+6)*i, 500, 80, 40);
button1.backgroundColor = [UIColor blackColor];
button1.alpha = 0.6;
button1.tag = 1000+i;
button1.layer.cornerRadius = 4;
button1.layer.masksToBounds = YES;
[button1 setTitle:anniu[i] forState:UIWindowLevelNormal];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button1 addTarget:self action:@selector(jifen:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
color = @[
[UIColor orangeColor],
[UIColor blackColor],
[UIColor blueColor]
];
// 颜色显示框
NO1 =[[UILabel alloc]init];
NO1.backgroundColor = [UIColor lightGrayColor];
NO1.frame = CGRectMake(200, 400, 100, 40);
NO1.center = CGPointMake(187.5, 320);
NO1.layer.cornerRadius = 4;
NO1.layer.masksToBounds = YES;
[self.view addSubview:NO1];

// 计数器相关
showTime = 60;
label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 0, 100, 100);
label.textAlignment = NSTextAlignmentCenter;
label.font =[UIFont systemFontOfSize:50];
label.center = CGPointMake(187.5, 250);
label.text = [@(showTime)stringValue];
[self.view addSubview:label];
// 定时器
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];
timer.fireDate = [NSDate distantFuture];
// 定时器1
timer1 = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(turn) userInfo:nil repeats:YES];
timer1.fireDate = [NSDate distantFuture];

// 积分显示框
jifen = [[UILabel alloc]init];
jifen.frame = CGRectMake(0, 0, 120, 50);
jifen.center = CGPointMake(187.5, 130);
jifen.textAlignment = NSTextAlignmentCenter;
jifen.font = [UIFont systemFontOfSize:40];
jifen.text = [@(b)stringValue];
jifen.layer.cornerRadius = 4;
jifen.layer.masksToBounds = YES;
jifen.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:jifen];

}
// turn 方法
- (void)turn
{
int i = arc4random()%color.count;

index = i;

NO1.text = @"颜色";
NO1.textAlignment = NSTextAlignmentCenter;
NO1.textColor = color[i];
if (showTime == 0) {
timer1.fireDate = [NSDate distantFuture];
showTime = 60;
b = 0;
jifen.text = [@(b)stringValue];
NO1.textColor = [UIColor blackColor];
}
}

// 开始按钮
- (void)start
{
timer.fireDate = [NSDate distantPast];
timer1.fireDate = [NSDate distantPast];
}

// change 方法
-(void)change
{
showTime --;

if (showTime == 0) {
timer.fireDate = [NSDate distantFuture];

UIAlertView *tishi = [[UIAlertView alloc]initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[tishi show];
}

label.text = [@(showTime)stringValue];

}

// 积分方法
- (void)jifen:(UIButton *)sender
{

if (sender.tag-1000 ==index) {
b += 5;
jifen.text = [@(b)stringValue];
}else{
b -=5;
jifen.text = [@(b)stringValue];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: