UI通过UISlider编写游戏第六感
#import "RootViewController.h"
@interface RootViewController ()
{
UILabel *scoreLabel;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//longSlider
UISlider *longSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 40)];
longSlider.tag = 101;
longSlider.maximumValue = 100;
longSlider.minimumValue = 0;
longSlider.userInteractionEnabled = NO;
longSlider.value = arc4random() % 101;
//[longSlider addTarget:self action:@selector(longSlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:longSlider];
[longSlider release];
//shortSlider
UISlider *shortSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width - 100, 40)];
shortSlider.tag = 102;
shortSlider.maximumValue = 100;
shortSlider.minimumValue = 0;
[self.view addSubview:shortSlider];
[shortSlider release];
UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 40, 40)];
showLabel.text = @"得分";
[self.view addSubview:showLabel];
[showLabel release];
scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
scoreLabel.tag = 103;
scoreLabel.layer.borderWidth = 2;
scoreLabel.layer.borderColor = [UIColor redColor].CGColor;
scoreLabel.layer.cornerRadius = 10;
[self.view addSubview:scoreLabel];
[scoreLabel release];
UIButton *okButton = [UIButton buttonWithType:UIButtonTypeSystem];
okButton.frame = CGRectMake(40, 300, 100, 50);
[okButton setTitle:@"OK" forState:UIControlStateNormal];
okButton.layer.borderWidth = 2;
okButton.layer.cornerRadius = 10;
[okButton addTarget:self action:@selector(okButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:okButton];
UIButton *againButton = [UIButton buttonWithType:UIButtonTypeSystem];
againButton.frame = CGRectMake(220, 300, 100, 50);
[againButton setTitle:@"重新" forState:UIControlStateNormal];
againButton.layer.borderWidth = 2;
againButton.layer.cornerRadius = 10;
[againButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:againButton];
// Do any additional setup after loading the view.
}
- (void)okButton:(UIButton *)aButton
{
UISlider *lSlider = (UISlider *)[self.view viewWithTag:101];
UISlider *sSlider = (UISlider *)[self.view viewWithTag:102];
UILabel *lable = (UILabel *)[self.view viewWithTag:103];
sSlider.userInteractionEnabled = NO;
if (lSlider.value == sSlider.value) {
lable.text = @"100";
UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不是人" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aa show];
[aa release];
} else {
float temp = 100 - abs((int)(lSlider.value - sSlider.value)) * 5;
if (temp > 90 && temp < 100) {
UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"提示" message:@"牛人" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aa show];
[aa release];
} else if (temp > 80 && temp < 90) {
UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"提示" message:@"眼力不错" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aa show];
[aa release];
} else if (temp > 60 && temp < 80) {
UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"提示" message:@"一般" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aa show];
[aa release];
} else {
UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"提示" message:@"眼瞎了吧!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[aa show];
[aa release];
}
scoreLabel.text = [NSString stringWithFormat:@"%.2f", temp];
}
}
- (void)pressButton
{
UISlider *slider = (UISlider *)[self.view viewWithTag:101];
//slider.value = arc4random() % 101;
[slider setValue:arc4random() % 61 + 20 animated:YES];
UISlider *sSlider = (UISlider *)[self.view viewWithTag:102];
sSlider.userInteractionEnabled = YES;
sSlider.value = 0;
UILabel *lable = (UILabel *)[self.view viewWithTag:103];
lable.text = @"0";
// [slider setValue:arc4random() animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end