UITouchView实现关灯游戏

来源:互联网 发布:android 7.0 数据库 编辑:程序博客网 时间:2024/06/11 21:12
MainViewControl.m
#import "MainViewController.h"#import "TouchView.h"@interface MainViewController ()@end@implementation MainViewController- (void) dealloc{    [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        TouchView * view = [[TouchView alloc] initWithFrame:CGRectMake(10, 20, 300, 440)];    [view setBackgroundColor:[UIColor blueColor]];    [self.view addSubview:view];    [view release];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
TouchView.m
#import "TouchView.h"@implementation TouchView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code        [self creatButton];    }        return self;}- (void)creatButton{    int tag = 101;    for (int i = 0; i < 8; i++) {        for (int j = 0; j < 6; j++) {            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(5+(50*j), 20+(i*50), 40, 40)];            [button setBackgroundColor:[UIColor yellowColor]];            [button setTag:tag++];            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];            [self addSubview:button];            [button release];            tag++;        }        tag += 5;    }}- (void)buttonAction:(id)sender{    UIButton * button = (UIButton *)sender;    UIButton * leftButton = (UIButton *)[self viewWithTag:button.tag - 1];    UIButton * rightButton = (UIButton *)[self viewWithTag:button.tag + 1];    UIButton * upButton = (UIButton *)[self viewWithTag:button.tag - 6];    UIButton * downButton = (UIButton *)[self viewWithTag:button.tag + 6];        if (button.backgroundColor == [UIColor yellowColor]) {        [button setBackgroundColor:[UIColor magentaColor]];    }else{        [button setBackgroundColor:[UIColor yellowColor]];    }    if (upButton.backgroundColor == [UIColor yellowColor]) {        [upButton setBackgroundColor:[UIColor magentaColor]];    }else{        [upButton setBackgroundColor:[UIColor yellowColor]];    }    if (downButton.backgroundColor == [UIColor yellowColor]) {        [downButton setBackgroundColor:[UIColor magentaColor]];    }else{        [downButton setBackgroundColor:[UIColor yellowColor]];    }    if (leftButton.backgroundColor == [UIColor yellowColor]) {        [leftButton setBackgroundColor:[UIColor magentaColor]];    }else{        [leftButton setBackgroundColor:[UIColor yellowColor]];    }    if (rightButton.backgroundColor == [UIColor yellowColor]) {        [rightButton setBackgroundColor:[UIColor magentaColor]];    }else{        [rightButton setBackgroundColor:[UIColor yellowColor]];    }    }@end
0 0
原创粉丝点击