#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *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 alloc] initWithFrame:[[UIScreen mainScreen] bounds]]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(0, 70, 320, 320)];
[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;
}
}