IOS简单关灯小游戏
2013-12-11 18:22
232 查看
在AppDelegate.m文件中添加视图控制器 #import "AppDelegate.h" #import "RootViewController.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]; RootViewController *rootVC = [[RootViewController alloc]init]; self.window.rootViewController = rootVC; [self.window makeKeyAndVisible]; return YES; } 在RootViewController.m中实现 RootViewController 继承于UIViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 300)]; [self.view addSubview:view]; CGFloat x,y; x=0,y=0; for (int i=0; i<5; i++) { x=0; //每次初始化x for (int j=0; j<5; j++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(x, y, 50, 50);// button.backgroundColor = [UIColor grayColor]; button.layer.contentsScale=4; button.layer.cornerRadius=14; [button addTarget:self action:@selector(createbutton:) forControlEvents:UIControlEventTouchUpInside]; button.tag = 10*(i+1)+j+1; //行每次+1,列+10 //NSLog(@"%ld",(long)button.tag); [view addSubview:button]; x+=50; } y+=50; } } - (void)createbutton:(UIButton *)selectedButton { // NSLog(@"%ld",(long)selectedButton.tag); //颜色调换 if (selectedButton.backgroundColor==[UIColor grayColor]) { selectedButton.backgroundColor = [UIColor yellowColor]; } else{ selectedButton.backgroundColor = [UIColor grayColor]; } //点击的button上下左右取反 UIButton *up=(UIButton*)[[self.view superview]viewWithTag:selectedButton.tag-10]; if (up.backgroundColor==[UIColor grayColor]) { up.backgroundColor = [UIColor yellowColor]; } else{ up.backgroundColor = [UIColor grayColor]; } UIButton *down=(UIButton*)[[self.view superview]viewWithTag:selectedButton.tag+10]; if (down.backgroundColor==[UIColor grayColor]) { down.backgroundColor = [UIColor yellowColor]; } else{ down.backgroundColor = [UIColor grayColor]; } UIButton *left=(UIButton*)[[self.view superview]viewWithTag:selectedButton.tag-1]; if (left.backgroundColor==[UIColor grayColor]) { left.backgroundColor = [UIColor yellowColor]; } else{ left.backgroundColor = [UIColor grayColor]; } UIButton *right=(UIButton*)[[self.view superview]viewWithTag:selectedButton.tag+1]; if (right.backgroundColor==[UIColor grayColor]) { right.backgroundColor = [UIColor yellowColor]; } else{ right.backgroundColor = [UIColor grayColor]; }
相关文章推荐
- IOS关灯小游戏
- iOS之关灯小游戏
- iOS中自定义UIImageView用TargetAction模式实现关灯小游戏
- iOS---实现在屏幕上实时绘图的简单效果---CAShaperLayer和UIBezierPath的简单运用
- iOS中实现简单易懂秒杀倒计时/倒计时代码
- iOS学习之UIPickerView控件的简单使用
- ios学习:多线程一(简单使用)
- iOS简单项目Demo-----贪吃蛇
- iOS简单排序--字母排序、NSDictionary排序
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- iOS 开发学习39 简单学习反射的部分用法
- IOS简单的画线功能实现
- iOS边练边学--文件压缩和解压缩的第三方框架SSZipArchive的简单使用
- iOS逆向工程(简单利用"dumpdecrypted"给ipa砸壳)
- iOS中GCD的简单概念
- 大钟的ios开发之旅(7)————简单谈谈GCD
- iOS 中快速简单高效的实现自定义tableViewCell 的方法-亲测实战版本-精华版
- 【iOS】最简单的方式实现跑马灯文字效果
- iOS中表格(UITableView)嵌套表格的简单实现
- iOS开发UI篇—UITabBarController简单介绍