實例一 find the monkey

UI比較基礎(chǔ)的都過了一遍,就找了一個開源的簡單項目運行了下然后看了一遍代碼,雖然小游戲很簡單,甚至都沒有用到Cocoa2d之類的,但還是很高興基本代碼都看懂了,通過查官方文檔也學(xué)習(xí)了一些新的用法。感覺通過小例子來學(xué)習(xí)是一個比較有意思的方式。
現(xiàn)在把自己注釋過的代碼貼上來,感覺非常適合看完教程后的第一個小游戲。

項目地址:https://github.com/rmd6502/monkey

monkeyViewController.h

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.Apple-tab-span {white-space:pre}

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface monkeyViewController : UIViewController<AVAudioPlayerDelegate> { //音頻播放協(xié)議
 NSUInteger hiddenLocation; //"藏猴子"的位置
 NSTimer *clock;            //計時用
 NSUInteger elapsed_seconds;
 AVAudioPlayer *avp;        //兩種音頻
 AVAudioPlayer *win;
}

@property (nonatomic, assign) IBOutlet UIButton *startButton;  //IBOutlet可視化編程自動生成變量
@property (nonatomic, assign) IBOutlet UITextField *timeRemaining;
@property (nonatomic, assign) IBOutlet UILabel *where;

- (IBAction)startGame:(id)sender;  //可視化編程自動生成事件函數(shù)
- (void)guess:(id)sender;          //傳遞猜的位置 并判斷正誤
- (void)tick:(NSTimer *)timer;     //計時
- (void)resetGame;                 //重新開始

@end

monkeyViewController.m

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s3 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s9 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s10 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s12 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s13 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s14 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s15 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.Apple-tab-span {white-space:pre}

#import <QuartzCore/QuartzCore.h>
#import "monkeyViewController.h"
//quartz 是主要的描畫接口,支持基于路徑的描畫、抗鋸齒渲染、漸變填充模式、圖像、顏色、坐標(biāo)空間變換、以及PDF 文檔的創(chuàng)建、顯示、和分析。UIKit 為Quartz 的圖像和顏色操作提供了Objective-C 的封裝。Core Animation 為很多UIKit 的視圖屬性聲明的動畫效果提供底層支持,也可以用于實現(xiàn)定制的動畫。

@implementation monkeyViewController
@synthesize startButton;
@synthesize timeRemaining;
@synthesize where;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void) viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //CGColor主要用于CoreGaphics框架之
 CGColorSpaceRef csp = CGColorSpaceCreateDeviceRGB();
 CGColorRef col1 = [UIColor cyanColor].CGColor; //cyanColor RGB=011 青色
 CGFloat comps[4] = {0};
    //內(nèi)存拷貝函數(shù),memcpy函數(shù)的功能是從源src所指的內(nèi)存地址的起始位置開始拷貝n個字節(jié)到目標(biāo)dest所指的內(nèi)存地址的起始位置中。
 memcpy(comps, CGColorGetComponents(col1), 4 * sizeof(CGFloat));
 comps[0] = 0.75;
 CGColorRef col2 = CGColorCreate(csp, comps);
    comps[0] = 0;
    for (int i=0; i < 3; ++i) comps[i] *= .7;
    col1 = CGColorCreate(csp, comps); //在csp空間中通過CGColorCreate用RGB創(chuàng)建顏色
 //CGColorRef col2 = [UIColor whiteColor].CGColor;
    
 const CGColorRef cols[] = {col1, col2};
    
    
    
 CFArrayRef collist = CFArrayCreate(nil, (const void **)cols, sizeof(cols)/sizeof(CGColorRef), NULL);
 CGFloat locs[] = {.99,0.0}; //?
 
 UIButton *b1 = (UIButton *)[self.view viewWithTag:1001];
 CGContextRef ctx = CGBitmapContextCreate(nil, b1.bounds.size.width, b1.bounds.size.height, 8, 4 * b1.bounds.size.width, csp, kCGImageAlphaPremultipliedLast);
 CGGradientRef grad = CGGradientCreateWithColors(csp, collist, locs);
 CGContextDrawRadialGradient(ctx, grad, CGPointMake(b1.bounds.size.width/2,b1.bounds.size.height/2), 1, CGPointMake(b1.bounds.size.width/2,b1.bounds.size.height/2), b1.bounds.size.width/2, kCGGradientDrawsAfterEndLocation);
 CGImageRef bg = CGBitmapContextCreateImage(ctx);
 UIImage *im = [UIImage imageWithCGImage:bg];
    
    
    
    //猜的格子設(shè)置為UIButton
    //用guess事件函數(shù)判斷猜對沒有
    //傳送過去的參數(shù)就是點擊的那個UIButton對象
 for (int i=1001; i < 1010; ++i) {
  UIButton *b = (UIButton *)[self.view viewWithTag:i];
  [b addTarget:self action:@selector(guess:) forControlEvents:UIControlEventTouchUpInside];
  [b setBackgroundImage:im forState:UIControlStateNormal];
 }
    
    
    //內(nèi)存釋放
 CGImageRelease(bg);
 CFRelease(collist);
 CGColorRelease(col2);
    CGColorRelease(col1);
 CGGradientRelease(grad);
 CGColorSpaceRelease(csp);
 [self resetGame];
    
    
    
    
 
    //載入音頻
 NSError *error = nil;
 NSURL *noiseURL = [[NSBundle mainBundle]    //猜錯的音樂
        URLForResource:@"buzzer" 
        withExtension:@"mp3"];
 avp = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:noiseURL error:&error];
 if (error) {
  NSLog(@"failed to load sound: %@", [error localizedDescription]);
  return;
 }
 noiseURL = [[NSBundle mainBundle]   //猜對的音樂
    URLForResource:@"win" 
    withExtension:@"m4a"];
 win = [[AVAudioPlayer alloc] 
     initWithContentsOfURL:noiseURL error:&error];
    //設(shè)置音頻屬性
 [avp setDelegate:self];
 [avp setVolume:1.0];
 [avp prepareToPlay];
 [win setDelegate:self];
 [win setVolume:1.0];
 [win prepareToPlay];
 
    [self becomeFirstResponder];//:第一響應(yīng)對象是窗口中,應(yīng)用程序認(rèn)為最適合處理事件的對象

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 NSLog(@"motion %@", event);
 if (motion == UIEventSubtypeMotionShake) {
  [self startGame:nil];
 }
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 NSLog(@"motion began %@", event);
 if (motion == UIEventSubtypeMotionShake) {
  [self startGame:nil];
 }
}

- (void) viewDidUnload {
 [avp release];
 [win release];
 [super viewDidUnload];
}

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 [self becomeFirstResponder];
}

//重新開始游戲
- (void)resetGame {
 CABasicAnimation *trans = [CABasicAnimation animation];
 trans.keyPath = @"transform.scale";
 trans.repeatCount = HUGE_VALF;
 trans.duration = 0.5;
 trans.autoreverses = YES;
 trans.removedOnCompletion = NO;
 trans.fillMode = kCAFillModeForwards;
 trans.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 trans.fromValue = [NSNumber numberWithFloat:0.9];
 trans.toValue = [NSNumber numberWithFloat:1.1];
 [self.startButton.titleLabel.layer addAnimation:trans forKey:@"pulse"];
 for (int i=1001; i < 1010; ++i) [(UIButton *)[self.view viewWithTag:i] setEnabled:NO];
 elapsed_seconds = 0;
}

//開始游戲
- (IBAction)startGame:(id)sender {  //觸發(fā)事件函數(shù)
 for (int i=1001; i < 1010; ++i) {
  UIButton *b = (UIButton *)[self.view viewWithTag:i]; //創(chuàng)建9個Button
  [b setImage:nil forState:UIControlStateNormal];
  [b setTitle:@"?" forState:UIControlStateNormal];   //顯示問號
  b.enabled = YES;
 }
 [self.startButton.titleLabel.layer removeAllAnimations];
    
 NSUInteger isCorrect = 0;
 SecRandomCopyBytes(kSecRandomDefault, sizeof(NSUInteger), (void *)&isCorrect); //產(chǎn)生隨機數(shù)
 hiddenLocation = isCorrect % 9;
 elapsed_seconds = 0;
 [timeRemaining setText:@"00:00:00"];
 if (clock) {
  [clock invalidate];
 }
 clock = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

- (BOOL)canBecomeFirstResponder { return YES; }

//猜猴子判斷是否猜中的事件函數(shù)
//由UIButton觸發(fā)
- (void)guess:(id)sender {
 UIButton *guessed = (UIButton *)sender;
 guessed.enabled = NO;
 
 CATransition *trans = [[CATransition alloc] init]; //CATransition轉(zhuǎn)場動畫
 trans.duration = 0.25;
//類型:
//    NSString * const kCATransitionFade;//逐漸消失
//    NSString * const kCATransitionMoveIn;//移入
//    NSString * const kCATransitionPush;//平移(暫且這么稱呼吧)
//    NSString * const kCATransitionReveal;//顯露
 trans.type = kCATransitionFade;
 [guessed.layer addAnimation:trans forKey:@"Fade"];
 [trans release];
    
    
 [CATransaction begin];//CATransaction 用來修改圖層樹
 if (guessed.tag - 1001 == hiddenLocation) { //hiddenLocation 猜對的位置
  [guessed setTitle:@"" forState:UIControlStateNormal]; //去掉?的title
  [guessed setImage:[UIImage imageNamed:@"monkey_toy"] forState:UIControlStateNormal];
  [clock invalidate];                     //停止計時
  clock = nil;
  [self resetGame];                       //重新開始
  [win play];
 } else {                                      //猜錯的位置
  [guessed setTitle:@"×" forState:UIControlStateNormal];
  [avp stop];
  [avp prepareToPlay];
  [avp play];
 }
 [CATransaction commit];
}

//計時
- (void)tick:(NSTimer *)timer {  //timeRemaining、elapsed_seconds都是自己定義的
 ++elapsed_seconds;
 [timeRemaining setText:[NSString stringWithFormat:@"%02d:%02d:%02d",
       elapsed_seconds / 3600, (elapsed_seconds % 3600) / 60, elapsed_seconds % 60]];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [super dealloc];
}
       

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,400評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,136評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,452評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,818評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,997評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,552評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,292評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,510評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,721評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,235評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,480評論 2 379

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,722評論 25 708
  • 從小我在英語學(xué)習(xí)上算是比較有天賦的。只是我既不喜歡也不討厭這門學(xué)科,準(zhǔn)確點說,是這門語言。 它有許...
    Ladyjack5閱讀 423評論 0 0
  • 體悟: 根據(jù)經(jīng)濟學(xué)人的語域,雖然將原文的隱喻翻出來能讓讀者體會到st 的風(fēng)采,但還是以傳遞信息為target, 建...
    野生的萱閱讀 198評論 0 0
  • 上一篇我們理解了什么是能力,那么當(dāng)我們掌握了能力,接下來我們應(yīng)該做什么呢,那就開始遷移你的能力吧。 能力遷移 能...
    秋月連江閱讀 445評論 2 2
  • 6月初的一個早晨,半醒狀態(tài)拿手機(iphone 5s)時,沒拿穩(wěn),手機摔了下去。biang的一聲過后,我習(xí)慣性的把...
    songkun閱讀 333評論 0 1