IOS UI 关灯游戏

来源:互联网 发布:连云港网络咨询招聘 编辑:程序博客网 时间:2024/04/30 08:16

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strongnonatomicUIWindow *window;


@end


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

#import "AppDelegate.h"

#import "mainViewController.h"

@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    _window = nil;

    [super dealloc];

}


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

{

    self.window = [[[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    

    mainViewController * main = [[mainViewController alloc]init];

    [_window setRootViewController:main];

    [main release];

    [self.window makeKeyAndVisible];

    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 = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    

    

    View * view = [[View alloc]initWithFrame:CGRectMake(070320320)];

    [view setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:view];

    [view release];

    

    

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // 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

{

    [super dealloc];

}



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        _lightArray = [[NSMutableArray alloc]init];

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

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

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

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

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

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

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

            [_lightArray addObject:button];

            [self addSubview:button];

            [button release];

            

        }


    }

    return self;

}


-(void)Action:(UIButton *)sender

{

    //改变状态

    sender.selected = !sender.selected;

    //打印sender下标

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

    //把下标存到index

    NSInteger index = [_lightArray indexOfObject:sender];

    if (index%5 != 0) {

        //获取左边的按钮

        UIButton * button = [_lightArray objectAtIndex:index-1];

        //改变状态(取反)

        button.selected =! button.selected;

    }

    if (index >= 5 ) {

        UIButton * button = [_lightArray objectAtIndex:index- 5];

        button.selected =! button.selected;

    }

    if (index%5 != 4) {

        UIButton * button =[_lightArray objectAtIndex:index + 1];

        button.selected =! button.selected;

    }

    if (index<20) {

        UIButton * button = [_lightArray objectAtIndex:index+5];

        button.selected =! button.selected;

    }

}


0 0
原创粉丝点击