您现在的位置是:首页 > 文章详情

基于oc的不完整对对碰

日期:2016-10-11点击:395

// // ViewController.m // 对对碰 // // Created by 杨晓鸣 on 16/9/19. // Copyright © 2016年 杨晓鸣. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSMutableArray* arrStr = [[NSMutableArray alloc]init]; for (int k = 0; k<18; k++) { int random = arc4random()%7+1; NSString* strName = [NSString stringWithFormat:@"%d",random]; [arrStr addObject:strName]; [arrStr addObject:strName]; } //循环创建36个按钮 for (int i = 0; i<6; i++) { for (int j = 0; j<6; j++) { UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom]; [UIView beginAnimations:nil context:nil]; btn.frame = CGRectMake(160, 160, 50, 50); [UIView setAnimationDuration:2]; btn.frame = CGRectMake(10+50*j, 40+50*i, 50, 50); [UIView commitAnimations]; //产生随机图片 int indexramdom = arc4random() % arrStr.count; //从图像数组中取出文件名 NSString* strImage = arrStr[indexramdom]; NSInteger tag = [strImage integerValue]; [arrStr removeObjectAtIndex:indexramdom]; UIImage* image = [UIImage imageNamed:strImage]; [btn setImage:image forState:UIControlStateNormal]; [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //将按钮的标志位赋值 btn.tag = tag; } } } -(void) pressBtn:(UIButton*) btn { //创建一个静态变量 保存第一次按下的按钮 static UIButton* btnFirst = nil; if (btnFirst == nil) { btnFirst = btn; //锁定第一个按钮 btnFirst.enabled = NO; } else{ //两个图片相同 if (btnFirst.tag == btn.tag) { btnFirst.hidden = YES; btn.hidden = YES; btnFirst = nil; } //按钮图片不相同 else{ btnFirst.enabled = YES; btnFirst = nil; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

原文链接:https://yq.aliyun.com/articles/61576
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章