//用到的UIButton+Create分類
#import "UIButton+Create.h"
@implementation UIButton (Create)
+ (UIButton *)buttonWithType:(UIButtonType)type frame:(CGRect)frame title:(NSString *)title target:(id)target action:(SEL)action backgroundColor:(UIColor *)backgroundColor alpha:(CGFloat)alpha
{
UIButton *button = [UIButton buttonWithType:type];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor=backgroundColor;
button.alpha=alpha;
button.layer.cornerRadius=10;
return button;
}
@end
//在視圖控制器創建TheFirstPass類一個的對象
- (void)viewDidLoad
{
[super viewDidLoad];
TheFirstPass *aview=[[TheFirstPass alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
aview.tag=100;
[self.view addSubview:aview];
[aview release];
// Do any additional setup after loading the view.
}
//實現
#import "TheFirstPass.h"
#import "UIButton+Create.h"
@implementation TheFirstPass
- (id)initWithFrame:(CGRect)frame
{
//非ARC模式
self = [super initWithFrame:frame];
if (self) {
//添加背景視圖
UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"12.jpg"]];
img.frame=CGRectMake(0, 0, 320, 480);
img.userInteractionEnabled=YES;
[self addSubview:img];
[img release];
//用分類創建button
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect frame:CGRectMake(10+j*60, 100+i*60, 60, 60) title:nil target:self action:@selector(changebackground:) backgroundColor:[UIColor cyanColor] alpha:0.9];
[button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateSelected];//選擇狀態的圖片
[self addSubview:button];
button.tag=(i+2)*10+(j+1);
}
}
//關數的標籤
_label=[[UILabel alloc]initWithFrame:CGRectMake(120, 20, 60, 30)];
_label.backgroundColor=[UIColor cyanColor];
_label.text=@"第1關";
_label.tag=10;
_label.textAlignment=NSTextAlignmentCenter;
_label.layer.cornerRadius=10;
[self addSubview:_label];
[_label release];
//步數的標籤
_label=[[UILabel alloc]initWithFrame:CGRectMake(120, 60, 60, 30)];
_label.backgroundColor=[UIColor cyanColor];
_label.text=@"第0步";
_label.tag=9;
_label.textAlignment=NSTextAlignmentCenter;
_label.layer.cornerRadius=10;
[self addSubview:_label];
[_label release];
//第一關初始設置
int j=(arc4random()%5+2)*10+(arc4random()%5+1);//隨機tag值
UIButton *but=(UIButton *)[self viewWithTag:j];
//tag值上下左右和自身的button取反
but.selected=!but.selected;
UIButton *up=(UIButton*)[self viewWithTag:but.tag-10];
up.selected=!up.selected;
UIButton *down=(UIButton*)[self viewWithTag:but.tag+10];
down.selected=!down.selected;
UIButton *left=(UIButton*)[self viewWithTag:but.tag-1];
left.selected=!left.selected;
UIButton *right=(UIButton*)[self viewWithTag:but.tag+1];
right.selected=!right.selected;
_km=1;//全局變量關數
_m=0;//全局變量步數
// Initialization code
}
return self;
}
-(void)succeed//判斷過關
{
int num=0;
for (int i=21; i<=65; i++) {
UIButton *current=(UIButton *)[self viewWithTag:i];
if (current.selected == YES) {
num++;
}
}
if (num==0) {
NSString *st=[NSString stringWithFormat:@"過關了,你用了%d步",_m];
NSString *s=[NSString stringWithFormat:@"馬上挑戰第%d關",(_km+1)];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:st message:s delegate:nil cancelButtonTitle:@"GO!" otherButtonTitles: nil];
[alertView show];
[alertView release];
_km++;
[self initialize];
NSString *str=[NSString stringWithFormat:@"第%d關",_km];
_label=(UILabel *)[self viewWithTag:10];
_label.text=str;
NSLog(@"------------*****");
_m=0;
}
}
-(void)initialize//關卡設置
{
for (int k=0; k<_km; k++) {
int j=(arc4random()%5+2)*10+(arc4random()%5+1);
NSLog(@"--------%d",j);
UIButton *but=(UIButton *)[self viewWithTag:j];
but.selected=!but.selected;
UIButton *up=(UIButton*)[self viewWithTag:but.tag-10];
up.selected=!up.selected;
UIButton *down=(UIButton*)[self viewWithTag:but.tag+10];
down.selected=!down.selected;
UIButton *left=(UIButton*)[self viewWithTag:but.tag-1];
left.selected=!left.selected;
UIButton *right=(UIButton*)[self viewWithTag:but.tag+1];
right.selected=!right.selected;
}
}
-(void)changebackground:(id)sender//點擊時取反
{
_m++;
NSString *strr=[NSString stringWithFormat:@"第%d步",_m];
_label=(UILabel *)[self viewWithTag:9];
_label.adjustsFontSizeToFitWidth=YES;
_label.text=strr;
UIButton*but=(UIButton*)sender;
NSLog(@"-----++++++%d",but.tag);
but.selected=!but.selected;
UIButton *up=(UIButton*)[self viewWithTag:but.tag-10];
up.selected=!up.selected;
UIButton *down=(UIButton*)[self viewWithTag:but.tag+10];
down.selected=!down.selected;
UIButton *left=(UIButton*)[self viewWithTag:but.tag-1];
left.selected=!left.selected;
UIButton *right=(UIButton*)[self viewWithTag:but.tag+1];
right.selected=!right.selected;
NSLog(@"************++++++++");
[self succeed];
}