关灯游戏

来源:互联网 发布:flag在c语言中的用法 编辑:程序博客网 时间:2024/04/27 14:26

#import "MainViewController.h"

#import "CustomView.h"


@interface MainViewController ()


@end


@implementation MainViewController

-(void)dealloc{

    [superdealloc];

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

        

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    

   CustomView * View = [[CustomViewalloc]initWithFrame:CGRectMake(25,50, 260, 265)];

        [View setBackgroundColor:[UIColorblueColor]];

        [self.viewaddSubview:View];

        [Viewrelease];

}







#import "CustomView.h"


@implementation CustomView

-(void)dealloc

{

    [superdealloc];

}


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        

        [selfcreatButton];

    }

    return self;

}


-(void)creatButton

{

   int tag = 101;

    

   for (int i =0; i < 5; i++) {

        

       for (int j =0; j < 5; j++) {

           UIButton * button =[[UIButtonalloc]initWithFrame:CGRectMake(10 +50 *j, 10 +50 * i, 40,40)];

            [button setBackgroundColor:[UIColorwhiteColor]];

            [button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

            [buttonsetTag:tag];

            [selfaddSubview:button];

            [buttonrelease];

            tag ++;

        }

        tag +=5;

    }

    

}


-(void)buttonAction:(id)sender

{

   UIButton * button = (UIButton *)sender;

   UIButton * leftButton = (UIButton *)[selfviewWithTag:button.tag -1];

   UIButton * rightButton = (UIButton *)[selfviewWithTag:button.tag +1];

   UIButton * downButton = (UIButton *)[selfviewWithTag:button.tag +10];

   UIButton * aboveButton = (UIButton *)[selfviewWithTag:button.tag -10];

    

    

   if (button.backgroundColor == [UIColorwhiteColor]) {

        [button setBackgroundColor:[UIColoryellowColor]];

    }else{

        [button setBackgroundColor:[UIColorwhiteColor]];

    }

   if (aboveButton.backgroundColor == [UIColorwhiteColor]) {

        [aboveButton setBackgroundColor:[UIColoryellowColor]];

    }else{

        [aboveButton setBackgroundColor:[UIColorwhiteColor]];

    }

   if (leftButton.backgroundColor == [UIColorwhiteColor]) {

        [leftButton setBackgroundColor:[UIColoryellowColor]];

    }else{

        [leftButton setBackgroundColor:[UIColorwhiteColor]];

    }

   if (rightButton.backgroundColor == [UIColorwhiteColor]) {

        [rightButton setBackgroundColor:[UIColoryellowColor]];

    }else{

        [rightButton setBackgroundColor:[UIColorwhiteColor]];

    }

   if (downButton.backgroundColor == [UIColorwhiteColor]) {

        [downButton setBackgroundColor:[UIColoryellowColor]];

    }else{

        [downButton setBackgroundColor:[UIColorwhiteColor]];

    }


0 0
原创粉丝点击