有两个图片


新建一个CloseLight类

CloseLight.h

import<UIKit/UIKit.h>@interfaceCloseLight:UIViewController@property(nonatomic,assign)NSIntegertag;@end

CloseLight.m

#import"CloseLight.h"@interfaceCloseLight()@end@implementationCloseLight-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];if(self){//Custominitialization}returnself;}-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorwhiteColor];//Doanyadditionalsetupafterloadingtheview.UIView*view=[[UIViewalloc]initWithFrame:CGRectMake(15,50,290,310)];view.backgroundColor=[UIColorbrownColor];view.layer.cornerRadius=10;[self.viewaddSubview:view];[viewrelease];_tag=10000;for(inti=0;i<5;i++){for(intj=0;j<5;j++){UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];[buttonsetFrame:CGRectMake(10+10*j+45*j,14+14*i+45*i,50,50)];//[buttonsetBackgroundColor:[UIColorredColor]];[buttonsetBackgroundImage:[UIImagep_w_picpathNamed:@"1"]forState:UIControlStateNormal];button.layer.cornerRadius=5;[buttonaddTarget:selfaction:@selector(gobutton:)forControlEvents:UIControlEventTouchUpInside];button.tag=_tag+1000*(i+1)+(j+1);[viewaddSubview:button];}}UIButton*button=[UIButtonbuttonWithType:UIButtonTypeSystem];[buttonsetTitle:@"开始游戏"forState:UIControlStateNormal];button.frame=CGRectMake(120,310,60,30);[buttonaddTarget:selfaction:NULLforControlEvents:UIControlEventTouchUpInside];[viewaddSubview:button];}-(void)change:(UIButton*)button{//UIColor*newColor=button.backgroundColor;UIImage*p_w_picpath=[buttonbackgroundImageForState:1];if(p_w_picpath==[UIImagep_w_picpathNamed:@"1"]){//[buttonsetBackgroundColor:[UIColorblackColor]];[buttonsetBackgroundImage:[UIImagep_w_picpathNamed:@"2"]forState:UIControlStateNormal];}elseif(p_w_picpath==[UIImagep_w_picpathNamed:@"2"]){//[buttonsetBackgroundColor:[UIColorredColor]];[buttonsetBackgroundImage:[UIImagep_w_picpathNamed:@"1"]forState:UIControlStateNormal];}}-(void)gobutton:(id)sender{UIButton*newbutton=(UIButton*)sender;[selfchange:(UIButton*)[self.viewviewWithTag:newbutton.tag]];[selfchange:(UIButton*)[self.viewviewWithTag:newbutton.tag+1]];[selfchange:(UIButton*)[self.viewviewWithTag:newbutton.tag-1]];[selfchange:(UIButton*)[self.viewviewWithTag:newbutton.tag+1000]];[selfchange:(UIButton*)[self.viewviewWithTag:newbutton.tag-1000]];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}



AppDelegate.m

#import"AppDelegate.h"#import"CloseLight.h"@implementationAppDelegate-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];//Overridepointforcustomizationafterapplicationlaunch.self.window.backgroundColor=[UIColorwhiteColor];[self.windowmakeKeyAndVisible];CloseLight*light=[[CloseLightalloc]init];self.window.rootViewController=light;[_windowrelease];returnYES;}-(void)dealloc{[_windowrelease];[superdealloc];}-(void)applicationWillResignActive:(UIApplication*)application{//Sentwhentheapplicationisabouttomovefromactivetoinactivestate.Thiscanoccurforcertaintypesoftemporaryinterruptions(suchasanincomingphonecallorSMSmessage)orwhentheuserquitstheapplicationanditbeginsthetransitiontothebackgroundstate.//Usethismethodtopauseongoingtasks,disabletimers,andthrottledownOpenGLESframerates.Gamesshouldusethismethodtopausethegame.}-(void)applicationDidEnterBackground:(UIApplication*)application{//Usethismethodtoreleasesharedresources,saveuserdata,invalidatetimers,andstoreenoughapplicationstateinformationtorestoreyourapplicationtoitscurrentstateincaseitisterminatedlater.//Ifyourapplicationsupportsbackgroundexecution,thismethodiscalledinsteadofapplicationWillTerminate:whentheuserquits.}-(void)applicationWillEnterForeground:(UIApplication*)application{//Calledaspartofthetransitionfromthebackgroundtotheinactivestate;hereyoucanundomanyofthechangesmadeonenteringthebackground.}-(void)applicationDidBecomeActive:(UIApplication*)application{//Restartanytasksthatwerepaused(ornotyetstarted)whiletheapplicationwasinactive.Iftheapplicationwaspreviouslyinthebackground,optionallyrefreshtheuserinterface.}-(void)applicationWillTerminate:(UIApplication*)application{//Calledwhentheapplicationisabouttoterminate.Savedataifappropriate.SeealsoapplicationDidEnterBackground:.}@end




附件:http://down.51cto.com/data/2364827