这是我自己用来练手的一个app,实现了基本的功能...感觉还不错,分享出来,欢迎批评指正(附下载地址,最下边)
// // ShuConfigView.h // ShuRect // // Created by 东 王 on 12-3-30. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import "GameView.h" @interface ShuConfigView : UIViewController { UIView *backView; UIView *btnView; UIButton *modeBtn; //模式按钮 UIButton *numberBtn; //数量按钮 UIButton *beginBtn; //开始按钮 UIImage *kidsImage; //幼儿模式图片 UIImage *memoryImage; //记忆模式图片 UIImage *adultImage; //成人模式图片 UIImage *numThreeImage; //3*3图片 UIImage *numFourImage; //4*4图片 UIImage *numFiveImage; //5*5图片 UIImage *gameBeginImage; //开始按钮图片 GameView *toGameView; int modeInt; int numInt; } @property (nonatomic, retain) UIButton *modeBtn; @property (nonatomic, retain) UIButton *numberBtn; @property (nonatomic, retain) UIButton *beginBtn; @property (nonatomic, retain) UIView *backView; @property (nonatomic, retain) UIView *btnView; @property (nonatomic, retain) UIImage *kidsImage; @property (nonatomic, retain) UIImage *memoryImage; @property (nonatomic, retain) UIImage *adultImage; @property (nonatomic, retain) UIImage *numThreeImage; @property (nonatomic, retain) UIImage *numFourImage; @property (nonatomic, retain) UIImage *numFiveImage; @property (nonatomic, retain) UIImage *gameBeginImage; @property (nonatomic, retain) GameView *toGameView; @end
// // ShuConfigView.m // ShuRect // // Created by 东 王 on 12-3-30. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "ShuConfigView.h" //#define kDuration 0.7 //动画持续播放时间 @implementation ShuConfigView @synthesize modeBtn,numberBtn,beginBtn; @synthesize backView,btnView; @synthesize kidsImage,memoryImage,adultImage,numThreeImage,numFourImage,numFiveImage,gameBeginImage; @synthesize toGameView; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization modeInt = 0; numInt = 3; backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; UIImage *backImage = [UIImage imageNamed:@"background.png"]; UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage]; [backView addSubview:backImageView]; kidsImage = [UIImage imageNamed:@"child_mode_n.png"]; memoryImage = [UIImage imageNamed:@"memory_mode_n.png"]; adultImage = [UIImage imageNamed:@"audlt_mode_n.png"]; numThreeImage = [UIImage imageNamed:@"level3_n.png"]; numFourImage = [UIImage imageNamed:@"level4_n.png"]; numFiveImage = [UIImage imageNamed:@"level5_n.png"]; gameBeginImage = [UIImage imageNamed:@"start_n.png"]; modeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [modeBtn setImage:kidsImage forState:UIControlStateNormal]; [modeBtn setFrame:CGRectMake(46, 200, 227, 50)]; [modeBtn addTarget:self action:@selector(modePressed:) forControlEvents:UIControlEventTouchUpInside]; [btnView addSubview:modeBtn]; numberBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [numberBtn setImage:numThreeImage forState:UIControlStateNormal]; [numberBtn setFrame:CGRectMake(46, 270, 227, 50)]; [numberBtn addTarget:self action:@selector(numPressed:) forControlEvents:UIControlEventTouchUpInside]; [btnView addSubview:numberBtn]; beginBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [beginBtn setImage:gameBeginImage forState:UIControlStateNormal]; [beginBtn setFrame:CGRectMake(46, 340, 227, 50)]; [beginBtn addTarget:self action:@selector(beginPressed:) forControlEvents:UIControlEventTouchUpInside]; [btnView addSubview:beginBtn]; } return self; } /* *********模式按钮按下 */ -(void)modePressed:(id)sender { UIButton *myBtn = (UIButton *)sender; modeInt ++; if (modeInt == 1) { [myBtn setImage:memoryImage forState:UIControlStateNormal]; } else if(modeInt == 2) { [myBtn setImage:adultImage forState:UIControlStateNormal]; } else { modeInt = 0; [myBtn setImage:kidsImage forState:UIControlStateNormal]; } NSLog(@"进来%d",modeInt); } /* *********开始按钮按下 */ -(void)beginPressed:(id)sender { toGameView = [[GameView alloc] init]; toGameView.modNum = modeInt; switch (numInt) { case 3: toGameView.rowNum = 3; break; case 4: toGameView.rowNum = 4; break; case 5: toGameView.rowNum = 5; break; default: break; } /* *另一种跳转效果 */ /*CATransition *animation = [CATransition animation]; animation.delegate = self; animation.duration = kDuration; animation.timingFunction = UIViewAnimationCurveEaseInOut; animation.type = kCATransitionFade; animation.subtype = kCATransitionFromLeft;*/ [toGameView resetGame:nil]; [self presentModalViewController:toGameView animated:YES]; } /* *********数量按钮按下 */ -(void)numPressed:(id)sender { UIButton *myBtn = (UIButton *)sender; if (numInt == 3) { numInt = 4; [myBtn setImage:numFourImage forState:UIControlStateNormal]; } else if (numInt == 4) { numInt = 5; [myBtn setImage:numFiveImage forState:UIControlStateNormal]; }else { numInt = 3; [myBtn setImage:numThreeImage forState:UIControlStateNormal]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:backView]; [self.view addSubview:btnView]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
// // GameView.m // ShuRect // // Created by 东 王 on 12-3-30. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "GameView.h" @interface GameView () -(void)initNumArray; -(void)numBtnPressed:(id)sender; @end @implementation GameView @synthesize gameBtnView,gameBackView; @synthesize gameNumArray; @synthesize gameNumBtn,redBtnImage,greenBtnImage; @synthesize resetGameBtn,resetBtnNormalImage,resetBtnPressedImage,backGameBtn,backBtnNormalImage,backBtnPressedImage,stillTime,beginTime; @synthesize rowNum,modNum; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization rowNum = 3; modNum = 0; currentNum = 1; timeInt = 3; timeFloat = 0.00; stillTime = [[UILabel alloc] initWithFrame:CGRectMake(110, 30, 100, 40)]; [stillTime setText:@"3"]; [stillTime setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]]; [stillTime setTextAlignment:UITextAlignmentCenter]; beginTime = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; [self initNumArray]; gameBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; gameBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; UIImage *backImage = [UIImage imageNamed:@"background.png"]; UIImageView *backImageView = [[UIImageView alloc] initWithImage:backImage]; [gameBackView addSubview:backImageView]; redBtnImage = [UIImage imageNamed:@"orange.png"]; greenBtnImage = [UIImage imageNamed:@"green.png"]; resetBtnNormalImage = [UIImage imageNamed:@"btn_replay_normal.png"]; resetBtnPressedImage = [UIImage imageNamed:@"btn_replay_pressed.png"]; resetGameBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [resetGameBtn setImage:resetBtnNormalImage forState:UIControlStateNormal]; [resetGameBtn setImage:resetBtnPressedImage forState:UIControlEventTouchDown]; [resetGameBtn setFrame:CGRectMake(240, 10, 60, 60)]; [resetGameBtn addTarget:self action:@selector(resetGame:) forControlEvents:UIControlEventTouchUpInside]; [gameBtnView addSubview:resetGameBtn]; backBtnNormalImage = [UIImage imageNamed:@"btn_back_normal.png"]; backBtnPressedImage = [UIImage imageNamed:@"btn_back_pressed.png"]; backGameBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [backGameBtn setImage:backBtnNormalImage forState:UIControlStateNormal]; [backGameBtn setImage:backBtnPressedImage forState:UIControlEventTouchDown]; [backGameBtn setFrame:CGRectMake(10, 10, 60, 60)]; [backGameBtn addTarget:self action:@selector(backGame:) forControlEvents:UIControlEventTouchUpInside]; [gameBackView addSubview:stillTime]; [gameBtnView addSubview:backGameBtn]; } return self; } /* *******倒数3秒开始 */ -(void)timerFireMethod:(NSTimer *)theTimer { if (timeInt == 1) { [beginTime invalidate]; if (modNum == 1) { [self initBtnRectMemory]; } NSLog(@"jieshu"); beginTime = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerFireMethodBegin:) userInfo:nil repeats:YES]; return; } timeInt--; [stillTime setText:[NSString stringWithFormat:@"%d",timeInt]]; } /* *******开始毫秒计时 */ -(void)timerFireMethodBegin:(NSTimer *)theTimer { timeFloat+=0.01; [stillTime setText:[NSString stringWithFormat:@"%.2f",timeFloat]]; } /* *******返回主菜单 */ -(void)backGame:(id)sender { /*ShuConfigView *backHome = [[ShuConfigView alloc] init]; backHome.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:backHome animated:YES];*/ self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self dismissModalViewControllerAnimated:YES]; } /* *******重新游戏 */ -(void)resetGame:(id)sender { if (gameNumBtn!=nil) { gameNumBtn = nil; [gameBtnView removeFromSuperview]; } currentNum = 1; NSLog(@"%d",rowNum); [self initNumArray]; [self initBtnRect]; [self.view addSubview:gameBtnView]; timeFloat = 0.00; timeInt = 3; [beginTime invalidate]; [stillTime setText:[NSString stringWithFormat:@"%d",timeInt]]; beginTime = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; } /* *******初始化数字按钮 */ -(void)initBtnRect { float btnWidth = (290-10*(rowNum-1))/rowNum; float btnHeight = (380-10*(rowNum-1))/rowNum; for (int i = 0, countNum = 1; i<rowNum; i++) { for (int j = 0; j<rowNum; j++) { gameNumBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [gameNumBtn setBackgroundImage:redBtnImage forState:UIControlStateNormal]; [gameNumBtn setTitle:[NSString stringWithFormat:@"%d",[[gameNumArray objectAtIndex:countNum-1] intValue]] forState:UIControlStateNormal]; [gameNumBtn setFrame:CGRectMake(15+(btnWidth+10)*j, 80+(btnHeight+10)*i, btnWidth, btnHeight)]; [gameNumBtn addTarget:self action:@selector(numBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; gameNumBtn.tag = [[gameNumArray objectAtIndex:countNum-1] intValue]; [gameBtnView addSubview:gameNumBtn]; countNum++; } } } /* *******记忆模式下再次初始化数字按钮 */ -(void)initBtnRectMemory { float btnWidth = (290-10*(rowNum-1))/rowNum; float btnHeight = (380-10*(rowNum-1))/rowNum; for (int i = 0, countNum = 1; i<rowNum; i++) { for (int j = 0; j<rowNum; j++) { gameNumBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [gameNumBtn setBackgroundImage:redBtnImage forState:UIControlStateNormal]; //[gameNumBtn setTitle:[NSString stringWithFormat:@"%d",[[gameNumArray objectAtIndex:countNum-1] intValue]] forState:UIControlStateNormal]; [gameNumBtn setFrame:CGRectMake(15+(btnWidth+10)*j, 80+(btnHeight+10)*i, btnWidth, btnHeight)]; [gameNumBtn addTarget:self action:@selector(numBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; gameNumBtn.tag = [[gameNumArray objectAtIndex:countNum-1] intValue]; [gameBtnView addSubview:gameNumBtn]; countNum++; } } } /* *******按下数字按钮时触发的函数 */ -(void)numBtnPressed:(id)sender { UIButton *myBtn = (UIButton *)sender; NSLog(@"myBtnNum:%d",myBtn.tag); if (timeFloat >= 0.01) { if (myBtn.tag == currentNum) { if (modNum == 0) { [myBtn setBackgroundImage:greenBtnImage forState:UIControlStateNormal]; } if (modNum == 1) { [myBtn setTitle:[NSString stringWithFormat:@"%d",myBtn.tag] forState:UIControlStateNormal]; } if (myBtn.tag == rowNum*rowNum) { [beginTime invalidate]; GameAlertC *alert = [[GameAlertC alloc] initGameAlert:@"alertBack.jpg" rect:CGRectMake(0, 0, 300, 300) title:[NSString stringWithFormat:@"恭喜你:%.2f秒",timeFloat]]; alert._delegate = self; [self.view addSubview:alert]; return; } currentNum ++; } } } /* *******点击弹出层按钮后响应函数 */ -(void) AfterClickBtn:(id)sender { UIButton * sc = (UIButton *)sender; if (sc.tag == 1) { [self dismissModalViewControllerAnimated:YES]; }else { [self resetGame:nil]; } } /* *******初始化随机数组 */ -(void)initNumArray { int array_insert; gameNumArray = [NSMutableArray arrayWithCapacity:rowNum*rowNum]; array_insert=(int)(arc4random() % (rowNum*rowNum))+1; [gameNumArray addObject:[NSNumber numberWithInt:array_insert]]; for (int i=0; i<rowNum*rowNum-1; i++) { while ([gameNumArray containsObject:[NSNumber numberWithInt:array_insert]]) { array_insert=(int)(arc4random() % (rowNum*rowNum))+1; } [gameNumArray addObject:[NSNumber numberWithInt:array_insert]]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"你好"); [self.view addSubview:gameBackView]; [self.view addSubview:gameBtnView]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
// // GameAlertC.m // ShuRect // // Created by 东 王 on 12-4-4. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "GameAlertC.h" @implementation GameAlertC @synthesize _delegate; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(id)initGameAlert:(NSString *)backImageStr rect:(CGRect)backRect title:(NSString *)alertTitleStr { self = [super initWithFrame:CGRectMake(0, 0, 320, 480)]; if (self) { self.userInteractionEnabled = YES; NSLog(@"RectHeight:%f",backRect.size.height*2/3); UIImageView *alertBackImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:backImageStr]]; alertBackImageView.userInteractionEnabled = YES; [alertBackImageView setFrame:backRect]; UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, backRect.size.width, backRect.size.height*2/3)]; [alertLabel setText:alertTitleStr]; [alertLabel setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]]; [alertLabel setTextAlignment:UITextAlignmentCenter]; alertLabel.center = alertBackImageView.center; [alertBackImageView addSubview:alertLabel]; alertBackImageView.center = self.center; //设置弹出层圆角及border颜色宽度 alertBackImageView.layer.cornerRadius = 8; alertBackImageView.layer.borderWidth = 2; alertBackImageView.layer.borderColor = [UIColor lightGrayColor].CGColor; alertBackImageView.layer.masksToBounds = YES; //设置弹出动画 CAKeyframeAnimation *animation; animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.duration = 0.3; animation.delegate = self; animation.removedOnCompletion = YES; animation.fillMode = kCAFillModeForwards; NSMutableArray *values = [NSMutableArray array]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; animation.values = values; [alertBackImageView.layer addAnimation:animation forKey:nil]; UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; okBtn.tag = 1; okBtn.frame = CGRectMake(alertBackImageView.frame.size.width/2-55, 6*alertBackImageView.frame.size.height/7-25, 50, 30); /*[okBtn setBackgroundColor:[UIColor yellowColor]]; okBtn.layer.cornerRadius = 8; okBtn.layer.masksToBounds = YES;*/ [okBtn setTitle:@"Menu" forState:UIControlStateNormal]; [okBtn addTarget:self action:@selector(popAlert:) forControlEvents:UIControlEventTouchUpInside]; [alertBackImageView addSubview:okBtn]; UIButton *againBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; againBtn.frame = CGRectMake(alertBackImageView.frame.size.width/2+45, 6*alertBackImageView.frame.size.height/7-25, 50, 30); /*[okBtn setBackgroundColor:[UIColor yellowColor]]; okBtn.layer.cornerRadius = 8; okBtn.layer.masksToBounds = YES;*/ [againBtn setTitle:@"Again" forState:UIControlStateNormal]; againBtn.tag = 2; [againBtn addTarget:self action:@selector(popAlert:) forControlEvents:UIControlEventTouchUpInside]; [alertBackImageView addSubview:againBtn]; [self addSubview:alertBackImageView]; } return self; } //点击弹出层按钮响应函数 -(void)popAlert:(id)sender { UIButton *switchBtn = (UIButton *)sender; [_delegate AfterClickBtn:switchBtn]; [self removeFromSuperview]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end