自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開(kāi)發(fā)之GameKit藍(lán)牙實(shí)例講解

移動(dòng)開(kāi)發(fā) iOS
iPhone開(kāi)發(fā)應(yīng)用中關(guān)于GameKit藍(lán)牙實(shí)例講解是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)GameKit藍(lán)牙實(shí)例,具體內(nèi)容來(lái)看本文詳解。

iPhone開(kāi)發(fā)應(yīng)用中關(guān)于GameKit藍(lán)牙實(shí)例講解是本文要介紹的內(nèi)容,主要是來(lái)了解并學(xué)習(xí)GameKit藍(lán)牙實(shí)例。介紹一下這個(gè)實(shí)例實(shí)現(xiàn)的是兩個(gè)帶有藍(lán)牙設(shè)備的touch之間的一個(gè)小游戲,在界面上有個(gè)可以響應(yīng)事件的UIView(之前說(shuō)過(guò))可以點(diǎn)擊,然后看誰(shuí)新達(dá)到WINNING_TAP_COUNT (游戲中一常量可以自己設(shè)置)誰(shuí)先達(dá)到誰(shuí)就贏了,然后通知對(duì)方。還要引入GameKit.framework框架
頭文件BlueToothViewController.h:

  1. //  
  2. //  
  3. // BlueToothViewController.h  
  4. // BlueTooth  
  5. //  
  6. // Created by mingchun liu on 09-11-24.  
  7. // Copyright sdie 2009. All rights reserved.  
  8. //  
  9.  
  10. #import <UIKit/UIKit.h> 
  11. #import <GameKit/GameKit.h> 
  12.  
  13. #define START_GAME_KEY @"startgame"  
  14. #define END_GAME_KEY @"endgame"  
  15. #define TAP_COUNT_KEY @"taps"  
  16. #define WINNING_TAP_COUNT 50  
  17.  
  18. #define AMIPHD_P2P_SESSION_ID @"amiphdp2p2"//這個(gè)是藍(lán)牙協(xié)議  
  19.  
  20. @interface BlueToothViewController : UIViewController<GKPeerPickerControllerDelegate,GKSessionDelegate>{  
  21.         BOOL actingAsHost;//是否提供服務(wù),客戶端還是服務(wù)器端  
  22.         int playerTapCount;//記錄玩家點(diǎn)擊次數(shù)  
  23.         int opponentTapCount;//對(duì)方點(diǎn)擊次數(shù)  
  24.         IBOutlet UILabel *playerTapCountLabel;//顯示玩家點(diǎn)擊次數(shù)  
  25.         IBOutlet UILabel *opponentTapCountLabel;//顯示對(duì)手點(diǎn)擊次數(shù)  
  26.         NSString *opponentID;//對(duì)方標(biāo)識(shí)符  
  27.         GKSession *gkSession;  
  28.           
  29.         IBOutlet UILabel *startQuitButton;//開(kāi)始退出按鈕  
  30. }  
  31.  
  32. @property BOOL actingAsHost;  
  33. @property int playerTapCount;  
  34. @property int opponentTapCount;  
  35. @property (nonatomic,retain) GKSession *gkSession;  
  36.  
  37. @property (nonatomic,retain) NSString *opponentID;  
  38.  
  39. @property (nonatomic,retain)UILabel *playerTapCountLabel;  
  40. @property (nonatomic,retain)UILabel *opponentTapCountLabel;  
  41.  
  42. @property (nonatomic,retain)UILabel *startQuitButton;  
  43.  
  44. -(IBAction) handleStartQuitTapped;//處理開(kāi)始退出操作  
  45. -(IBAction) handleTapViewTapped;//處理點(diǎn)擊UIView的操作  
  46. -(void) updateTapCountLabels;//更新顯示  
  47. -(void) initGame;//初始化游戲  
  48. -(void) hostGame;  
  49. -(void) joinGame;//加入游戲  
  50. -(void) endGame;//結(jié)束游戲  
  51. -(void) showEndGameAlert;//彈出結(jié)束游戲?qū)υ捒? 
  52. @end  
  53.  
  54. #import "BlueToothViewController.h"  
  55.  
  56. @implementation BlueToothViewController  
  57.  
  58. @synthesize actingAsHost;  
  59. @synthesize playerTapCount;  
  60. @synthesize opponentID;  
  61. @synthesize playerTapCountLabel;  
  62. @synthesize opponentTapCountLabel;  
  63.  
  64. @synthesize startQuitButton;  
  65. @synthesize gkSession;  
  66. @synthesize opponentTapCount;  
  67.  
  68. -(IBAction) handleStartQuitTapped {//建立鏈接操作,彈出鏈接窗口顯示在線  
  69.         if (! opponentID) {//如果對(duì)手ID為空就建立服務(wù)端提供服務(wù)  
  70.                 actingAsHost = YES;  
  71.                 GKPeerPickerController *peerPickerController =[[GKPeerPickerController alloc] init];  
  72.                 peerPickerController.delegate = self;  
  73.                 peerPickerController.connectionTypesMask =  
  74.                 GKPeerPickerConnectionTypeNearby;  
  75.                 [peerPickerController show];  
  76.         }  
  77. }  
  78. -(IBAction) handleTapViewTapped {//點(diǎn)擊操作  
  79.         playerTapCount++;  
  80.         [self updateTapCountLabels];  
  81.         // did we just win?  
  82.         BOOL playerWins = playerTapCount >= WINNING_TAP_COUNT;//當(dāng)點(diǎn)擊達(dá)到一定次數(shù)時(shí)  
  83.         // send tap count to peer  
  84.         NSMutableData *message = [[NSMutableData alloc] init];//傳的數(shù)據(jù)類型為nsdata類型的  
  85.         NSKeyedArchiver *archiver =  
  86.         [[NSKeyedArchiver alloc] initForWritingWithMutableData:message];  
  87.         [archiver encodeInt:playerTapCount forKey: TAP_COUNT_KEY];  
  88.         if (playerWins)  
  89.                 [archiver encodeBool:YES forKey:END_GAME_KEY];  
  90.         [archiver finishEncoding];//打包傳數(shù)據(jù)  
  91.         GKSendDataMode sendMode =  
  92.         playerWins ? GKSendDataReliable : GKSendDataUnreliable;//判斷用可靠的鏈接還是不可靠的鏈接  
  93.         [gkSession sendDataToAllPeers: message withDataMode:sendMode error:NULL];//發(fā)送數(shù)據(jù)  
  94.         [archiver release];  
  95.         [message release];  
  96.         // also end game locally  
  97.         if (playerWins)  
  98.                 [self endGame];  
  99. }  
  100.  
  101. -(void) updateTapCountLabels {  
  102.         playerTapCountLabel.text =  
  103.         [NSString stringWithFormat:@"%d", playerTapCount];  
  104.         opponentTapCountLabel.text =  
  105.         [NSString stringWithFormat:@"%d", opponentTapCount];  
  106. }  
  107. -(void) initGame {  
  108.         playerTapCount = 0;  
  109.         opponentTapCount = 0;  
  110. }  
  111. -(void) hostGame {  
  112.         [self initGame];  
  113.         NSMutableData *message = [[NSMutableData alloc] init];  
  114.         NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]  
  115.                                                                  initForWritingWithMutableData:message];  
  116.         [archiver encodeBool:YES forKey:START_GAME_KEY];  
  117.         [archiver finishEncoding];  
  118.         NSError *sendErr = nil;  
  119.         [gkSession sendDataToAllPeers: message  
  120.                                          withDataMode:GKSendDataReliable error:&sendErr];  
  121.         if (sendErr)  
  122.                 NSLog (@"send greeting failed: %@", sendErr);  
  123.         // change state of startQuitButton  
  124.         startQuitButton.text = @"Quit";  
  125.         [message release];  
  126.         [archiver release];  
  127.         [self updateTapCountLabels];  
  128. }  
  129. -(void) joinGame {  
  130.         [self initGame];  
  131.         startQuitButton.text = @"Quit";  
  132.         [self updateTapCountLabels];  
  133. }  
  134.  
  135. //一下是代理方法  
  136.  
  137. -(GKSession *) peerPickerController: (GKPeerPickerController*) controller  
  138.                   sessionForConnectionType: (GKPeerPickerConnectionType) type {  
  139.         if (!gkSession) {//如果沒(méi)有鏈接時(shí)建立連接  
  140.                 gkSession = [[GKSession alloc]  
  141.                                          initWithSessionID:AMIPHD_P2P_SESSION_ID//根據(jù)此值判斷用的是什么鏈接  
  142.                                          displayName:nil//在線用戶名  
  143.                                          sessionMode:GKSessionModePeer];  
  144.                 gkSession.delegate = self;  
  145.         }  
  146.         return gkSession;  
  147. }  
  148.  
  149. - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session  
  150. {//當(dāng)picker接收到數(shù)據(jù)后將其釋放掉,否則進(jìn)入不了界面  
  151.         [picker dismiss];  
  152.         picker.delegate = nil;  
  153.         [picker autorelease];  
  154. }  
  155. - (void)session:(GKSession *)session  
  156. didReceiveConnectionRequestFromPeer:(NSString *)peerID {//已接受連接請(qǐng)求的代理方法  
  157.         actingAsHost = NO;//設(shè)為客戶端  
  158. }  
  159.  
  160. - (void)session:(GKSession *)session peer:(NSString *)peerID  
  161. didChangeState:(GKPeerConnectionState)state {//狀態(tài)改變時(shí)觸發(fā)的代理方法  
  162.         switch (state)  
  163.         {  
  164.                 case GKPeerStateConnected:  
  165.                         [session setDataReceiveHandler: self withContext: nil];  
  166.                         opponentID = peerID;//改變opponentID的值  
  167.                         actingAsHost ? [self hostGame] : [self joinGame];//  
  168.                         break;  
  169.         }  
  170. }  
  171.  
  172. - (void) receiveData: (NSData*) data fromPeer: (NSString*) peerID  
  173.                    inSession: (GKSession*) session context: (void*) context {//接受數(shù)據(jù)時(shí)的代理操作  
  174.         NSKeyedUnarchiver *unarchiver =  
  175.         [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  176.         if ([unarchiver containsValueForKey:TAP_COUNT_KEY]) {  
  177.                 opponentTapCount = [unarchiver decodeIntForKey:TAP_COUNT_KEY];  
  178.                 [self updateTapCountLabels];  
  179.         }  
  180.         if ([unarchiver containsValueForKey:END_GAME_KEY]) {  
  181.                 [self endGame];  
  182.         }  
  183.         if ([unarchiver containsValueForKey:START_GAME_KEY]) {  
  184.                 [self joinGame];  
  185.         }  
  186.         [unarchiver release];  
  187. }  
  188. //以上是代理方法  
  189.  
  190. -(void) showEndGameAlert {  
  191.         BOOL playerWins = playerTapCount > opponentTapCount;  
  192.         UIAlertView *endGameAlert = [[UIAlertView alloc]  
  193.                                                                  initWithTitle: playerWins ? @"Victory!" : @"Defeat!"  
  194.                                                                  message: playerWins ? @"Your thumbs have emerged supreme!":  
  195.                                                                  @"Your thumbs have been laid low"  
  196.                                                                  delegate:nil  
  197.                                                                  cancelButtonTitle:@"OK"  
  198.                                                                  otherButtonTitles:nil];  
  199.         [endGameAlert show];  
  200.         [endGameAlert release];  
  201. }  
  202. -(void) endGame {  
  203.         opponentID = nil;  
  204.         startQuitButton.text = @"Find";  
  205.         [gkSession disconnectFromAllPeers];  
  206.         [self showEndGameAlert];  
  207. }  
  208.  
  209. /*  
  210. // The designated initializer. Override to perform setup that is required before the view is loaded.  
  211. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {  
  212.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {  
  213.         // Custom initialization  
  214.     }  
  215.     return self;  
  216. }  
  217. */  
  218.  
  219. /*  
  220. // Implement loadView to create a view hierarchy programmatically, without using a nib.  
  221. - (void)loadView {  
  222. }  
  223. */  
  224.  
  225. /*  
  226. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  227. - (void)viewDidLoad {  
  228.     [super viewDidLoad];  
  229. }  
  230. */  
  231.  
  232. /*  
  233. // Override to allow orientations other than the default portrait orientation.  
  234. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  235.     // Return YES for supported orientations  
  236.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  237. }  
  238. */  
  239.  
  240. - (void)didReceiveMemoryWarning {  
  241.         // Releases the view if it doesn't have a superview.  
  242.     [super didReceiveMemoryWarning];  
  243.           
  244.         // Release any cached data, images, etc that aren't in use.  
  245. }  
  246.  
  247. - (void)viewDidUnload {  
  248.         // Release any retained subviews of the main view.  
  249.         // e.g. self.myOutlet = nil;  
  250. }  
  251.  
  252. - (void)dealloc {  
  253.         [opponentID release];  
  254.         [playerTapCountLabel release];  
  255.         [opponentTapCountLabel release];  
  256.  
  257.  
  258.         [startQuitButton release];  
  259.         [gkSession release];  
  260.     [super dealloc];  

小結(jié):iPhone開(kāi)發(fā)GameKit藍(lán)牙實(shí)例講解的內(nèi)容介紹完 ,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: aisidachina
相關(guān)推薦

2011-07-06 16:15:46

iPhone 圖片

2011-08-03 16:01:24

iPhone應(yīng)用開(kāi)發(fā) 自動(dòng)登陸

2011-07-25 18:02:51

iPhone LibFetion 移植

2013-05-21 09:56:15

2011-08-08 16:56:44

iPhone 字符處理 視圖

2011-07-29 13:27:48

iPhone 開(kāi)發(fā) Nib

2011-08-01 18:27:58

iPhone開(kāi)發(fā) UISearchBa

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-08 15:56:18

iPhone 震動(dòng) NSUserDefa

2011-07-27 11:19:33

iPhone UITableVie

2011-07-28 10:11:54

iPhone開(kāi)發(fā) 備忘

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫(kù)

2011-08-08 13:57:19

iPhone開(kāi)發(fā) 打包 DEB

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-22 15:15:49

iPhone開(kāi)發(fā)NSMutableAr排序

2013-07-23 07:34:54

iOS開(kāi)發(fā)學(xué)習(xí)適配iphone5

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-07-18 14:39:53

iPhone SDK UIKit

2011-08-16 18:42:42

iPhone開(kāi)發(fā)Release

2011-07-27 09:33:14

iPhone 網(wǎng)絡(luò) Web
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)