基于oc的不完整对对碰
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于oc的不完整对对碰相关的知识,希望对你有一定的参考价值。
//
// 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
以上是关于基于oc的不完整对对碰的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode栈与队列150. 逆波兰表达式求值(后缀表达式求值!!看作对对碰游戏!!)
蓝桥杯国赛真题20Scratch纸牌对对碰 青少年组 scratch蓝桥杯国赛真题和答案讲解