您好,登錄后才能下訂單哦!
有兩個圖片
新建一個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 p_w_picpathNamed:@"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 *p_w_picpath = [button backgroundImageForState:1]; if (p_w_picpath == [UIImage p_w_picpathNamed:@"1"]) { // [button setBackgroundColor:[UIColor blackColor]]; [button setBackgroundImage:[UIImage p_w_picpathNamed:@"2"] forState:UIControlStateNormal]; }else if(p_w_picpath == [UIImage p_w_picpathNamed:@"2"]){ // [button setBackgroundColor:[UIColor redColor]]; [button setBackgroundImage:[UIImage p_w_picpathNamed:@"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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。