UITouchView實現關燈遊戲

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章