iOS開發(fā)實戰(zhàn):搖一搖結(jié)合iBEacon

這是16年初的文章了,剛出自己的新浪搬過來盗冷!

搖一搖做起來很簡單缆八,但是再加上iBEacon,處理起來就沒有那么得心應(yīng)手了

- (void)viewDidAppear:(BOOL)animated

{

??[superviewDidAppear:animated];

?//?掃描iBEacon成為第一響應(yīng)

??[selfbecomeFirstResponder];

}

- (void)viewDidLoad {

//?搖一搖成為第一響應(yīng)

??[[UIApplicationsharedApplication]setApplicationSupportsShakeToEdit:YES];

??[superviewDidLoad];

//?搖一搖音效

?NSString*path = [[NSBundlemainBundle]pathForResource:@"shake"ofType:@"wav"];

?AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:path], &soundID);

?NSString*pathFinish = [[NSBundlemainBundle]pathForResource:@"shake_match"ofType:@"mp3"];

?AudioServicesCreateSystemSoundID((__bridgeCFURLRef)[NSURLfileURLWithPath:pathFinish], &soundFinishID);

?self.manager=[[CBPeripheralManageralloc]initWithDelegate:selfqueue:nil];

}

#pragma mark -搖一搖

搖一搖開始

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event

{


?if(motion ==UIEventSubtypeMotionShake) {

???NSLog(@"搖一搖");

?????AudioServicesPlaySystemSound(soundID);

//?掃描iBEacon初始化

??????[selfsetLocationInfo];


??}

}

-(void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event

{

?//搖動取消


?DLog(@"搖動取消");


}

-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event

{

?//搖動結(jié)束

?if(event.subtype

???==UIEventSubtypeMotionShake) {


?????AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


??????[self.viewshowLoadingViewWithText:@"正在匹配檢查報到信息"];



???DLog(@"搖動結(jié)束");


??}

}

掃描iBEacon需要打開藍牙

#pragma mark -檢測藍牙是否開啟

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral{

?switch(peripheral.state) {

?????//藍牙開啟且可用

???caseCBPeripheralManagerStatePoweredOn:

????{

?????NSLog(@"Bluetooth is currently powered on and available to use.");

?????_isRock=YES;

????}

?????break;

???default:

?????NSLog(@"Peripheral Manager did change state");

?????_isRock=NO;

?????break;

??}

}

#pragma mark -搖一搖動畫

-(void)addAnimations

{

?CABasicAnimation*translation = [CABasicAnimationanimationWithKeyPath:@"transform"];

??translation.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

??translation.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(-M_PI_4,0,0,100)];

??translation.duration=0.1;

??translation.repeatCount=8;

??translation.autoreverses=YES;


??[shake.layeraddAnimation:translationforKey:@"translation"];

}

#pragma mark -iBeacon方法

- (void)setLocationInfo{


?self.locationManager= [[CLLocationManageralloc]init];


?//60B48397-9C07-57EA-7F63-79999272FEF8 -- bsc

?if(_ibeaconArr.count!=0) {

???_ibeaconModel=_ibeaconArr[0];

??}

?NSUUID*uuid = [[NSUUIDalloc]initWithUUIDString:_ibeaconModel.uuId];

?//FDA50693-A4E2-4FB1-AFCF-C6EB07647825--返回數(shù)據(jù)


?// Setup a new region with that UUID and same identifier as the broadcasting beacon

?self.myBeaconRegion= [[CLBeaconRegionalloc]initWithProximityUUID:uuid

?????????????????????????????identifier:@"hospitalBooking"];


?self.locationManager.delegate=self;

?DLog(@"------%@",self.myBeaconRegion);


?if(iOS8orLater) {

????[self.locationManagerrequestAlwaysAuthorization];//設(shè)置location是一直允許

??}


??[self.locationManagerstartMonitoringForRegion:self.myBeaconRegion];




}

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region

{?//開始RegionBeacons

??[self.locationManagerstartRangingBeaconsInRegion:self.myBeaconRegion];


}

//找到iBeacon后掃描它的信息

-(void)locationManager:(CLLocationManager*)manager

???didRangeBeacons:(NSArray*)beacons

???????inRegion:(CLBeaconRegion*)region

{


?//?????CheckRegisterDetailViewController *check = [[CheckRegisterDetailViewController alloc] init];

?//?????[self.navigationController pushViewController:check animated:YES];


?DLog(@"%@",beacons);


?CLBeacon*foundBeacon = [beaconsfirstObject];


?//??You can retrieve the beacon data from its properties

?_hospitalUUId= foundBeacon.proximityUUID.UUIDString;

?_hospitalMajor= [NSStringstringWithFormat:@"%@", foundBeacon.major];

?_hospitalMinor= [NSStringstringWithFormat:@"%@", foundBeacon.minor];


?for(inti =0; i<_ibeaconArr.count; i++) {


???_ibeaconModel=_ibeaconArr[i];


???if([_hospitalMinorisEqualToString:_ibeaconModel.minorId] && [_hospitalMajorisEqualToString:_ibeaconModel.majorId]) {


?????_isSameRow= i;


?????_isSuccess=YES;


?????_getDataCount+=1;


????}


??}

?_currentTimestamp= [[NSDatedate]timeIntervalSince1970];

?doublesecond =_currentTimestamp-_beginTimestamp;

?if(second <15) {

???_isLook=YES;

??}

?NSLog(@"%d%f",_isLook,second);

?if(_isSuccess==YES) {


????[self.locationManagerstopRangingBeaconsInRegion:self.myBeaconRegion];

??}

?_isShankCount++;

?//查找到相同的iBeacon就調(diào)用

?if(_isSuccess==YES&&_getDataCount==1&&_isShankCount==1&&_isLook==YES) {


???_ibeaconModel=_ibeaconArr[_isSameRow];


???//查詢待簽到的檢查訂單


????[selfgetIbeaconCanBookDataWithIbeaconId:_ibeaconModel.id];


??}

?else{

????[self.viewdismissLoadingView];

//????UIAlertView *noteAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"掃描設(shè)備失敗垃杖,請重新?lián)u一搖" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];

//????[noteAlertView show];

???_isRock=YES;

???_isShankCount=0;


??}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市丈屹,隨后出現(xiàn)的幾起案子调俘,更是在濱河造成了極大的恐慌,老刑警劉巖旺垒,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彩库,死亡現(xiàn)場離奇詭異,居然都是意外死亡先蒋,警方通過查閱死者的電腦和手機骇钦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來竞漾,“玉大人眯搭,你說我怎么就攤上這事∫邓辏” “怎么了鳞仙?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長笔时。 經(jīng)常有香客問我棍好,道長,這世上最難降的妖魔是什么允耿? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任借笙,我火速辦了婚禮,結(jié)果婚禮上较锡,老公的妹妹穿的比我還像新娘提澎。我一直安慰自己,他們只是感情好念链,可當我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般掂墓。 火紅的嫁衣襯著肌膚如雪谦纱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天君编,我揣著相機與錄音跨嘉,去河邊找鬼。 笑死吃嘿,一個胖子當著我的面吹牛祠乃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兑燥,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼亮瓷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了降瞳?” 一聲冷哼從身側(cè)響起嘱支,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挣饥,沒想到半個月后除师,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡扔枫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年汛聚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片短荐。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡倚舀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出搓侄,到底是詐尸還是另有隱情瞄桨,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布讶踪,位于F島的核電站芯侥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏乳讥。R本人自食惡果不足惜柱查,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望云石。 院中可真熱鬧唉工,春花似錦、人聲如沸汹忠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至谣膳,卻和暖如春竿报,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背继谚。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工烈菌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人花履。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓芽世,卻偏偏與公主長得像,于是被迫代替她去往敵國和親诡壁。 傳聞我的和親對象是個殘疾皇子济瓢,可洞房花燭夜當晚...
    茶點故事閱讀 45,066評論 2 355