這是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;
??}
}