有两个图片
新建一个CloseLight类
CloseLight.h
import <UIKit/UIKit.h> @interface CloseLight : UIViewController @property (nonatomic , assign)NSInteger tag; @end
CloseLight.m
#import "CloseLight.h" @interface CloseLight () @end @implementation CloseLight - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; // Do any additional setup after loading the view. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 50, 290, 310)]; view.backgroundColor = [UIColor brownColor]; view.layer.cornerRadius = 10; [self.view addSubview:view]; [view release]; _tag = 10000; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j ++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setFrame:CGRectMake(10 + 10 * j + 45 * j, 14 + 14 * i + 45 * i, 50, 50)]; // [button setBackgroundColor:[UIColor redColor]]; [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal]; button.layer.cornerRadius = 5; [button addTarget:self action:@selector(gobutton:) forControlEvents:UIControlEventTouchUpInside]; button.tag = _tag +1000 *(i + 1) + (j + 1); [view addSubview:button]; } } UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"开始游戏" forState:UIControlStateNormal]; button.frame = CGRectMake(120, 310, 60, 30); [button addTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside]; [view addSubview:button]; } - (void)change:(UIButton *)button { // UIColor *newColor = button.backgroundColor; UIImage *image = [button backgroundImageForState:1]; if (image == [UIImage imageNamed:@"1"]) { // [button setBackgroundColor:[UIColor blackColor]]; [button setBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal]; }else if(image == [UIImage imageNamed:@"2"]){ // [button setBackgroundColor:[UIColor redColor]]; [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal]; } } - (void)gobutton:(id)sender { UIButton *newbutton = (UIButton *)sender; [self change:(UIButton *)[self.view viewWithTag:newbutton.tag] ]; [self change:(UIButton *)[self.view viewWithTag:newbutton.tag + 1]]; [self change:(UIButton *)[self.view viewWithTag:newbutton.tag - 1]]; [self change:(UIButton *)[self.view viewWithTag:newbutton.tag + 1000]]; [self change:(UIButton *)[self.view viewWithTag:newbutton.tag - 1000]]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
AppDelegate.m
#import "AppDelegate.h" #import "CloseLight.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; CloseLight *light = [[CloseLight alloc] init]; self.window.rootViewController = light; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
时间: 2024-11-06 09:29:44