OC关灯游戏

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

#import <UIKit/UIKit.h>


@interface AppDelegate :UIResponder <UIApplicationDelegate>


@property (strong,nonatomic) UIWindow *window;


@end


----------------------------分割线---------------------------------------------------

#import "AppDelegate.h"

#import "mainViewController.h"

@implementation AppDelegate

- (void)dealloc

{

    [_windowrelease];

   _window = nil;

    [superdealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    

    mainViewController * main = [[mainViewControlleralloc]init];

    [_windowsetRootViewController:main];

    [mainrelease];

    [self.windowmakeKeyAndVisible];

    return YES;

}

----------------------------分割线---------------------------------------------------

#import <UIKit/UIKit.h>


@interface mainViewController :UIViewController


@end

----------------------------分割线---------------------------------------------------


#import "mainViewController.h"

#import "View.h"

@interface mainViewController ()


@end


@implementation mainViewController


- (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.

    

    

   View * view = [[Viewalloc]initWithFrame:CGRectMake(0,70, 320, 320)];

    [view setBackgroundColor:[UIColorblackColor]];

    [self.viewaddSubview:view];

    [viewrelease];

    

    

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


----------------------------分割线---------------------------------------------------

#import <UIKit/UIKit.h>


@interface View : UIView

@property(retain,nonatomic)NSMutableArray * lightArray;


@end


----------------------------分割线---------------------------------------------------

#import "View.h"


@implementation View

- (void)dealloc

{

    [superdealloc];

}



- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        

       _lightArray = [[NSMutableArrayalloc]init];

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

           float x = 15 + (i %5) * 60;

           float y = 15 + (i /5) * 60;

           UIButton * button = [[UIButtonalloc]initWithFrame:CGRectMake(x, y,50, 50)];

            [button setImage:[UIImageimageNamed:@"110.png"]forState:UIControlStateNormal];

            [button setImage:[UIImageimageNamed:@"119.png"]forState:UIControlStateSelected];

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

            [_lightArrayaddObject:button];

            [selfaddSubview:button];

            [buttonrelease];

            

        }


    }

    return self;

}


-(void)Action:(UIButton *)sender

{

    //改变状态

    sender.selected = !sender.selected;

    //打印sender下标

   NSLog(@"%d",[_lightArrayindexOfObject:sender]);

    //把下标存到index

   NSInteger index = [_lightArrayindexOfObject:sender];

   if (index%5 !=0) {

        //获取左边的按钮

       UIButton * button = [_lightArrayobjectAtIndex:index-1];

        //改变状态(取反)

        button.selected =! button.selected;

    }

   if (index >= 5 ) {

       UIButton * button = [_lightArrayobjectAtIndex:index- 5];

        button.selected =! button.selected;

    }

   if (index%5 !=4) {

       UIButton * button =[_lightArrayobjectAtIndex:index + 1];

        button.selected =! button.selected;

    }

   if (index<20) {

       UIButton * button = [_lightArrayobjectAtIndex:index+5];

        button.selected =! button.selected;

    }

}










0 0