iOS开发进阶

iOS开发实战:摇一摇结合iBEacon

2019-07-31  本文已影响0人  文竹_自然

这是16年初的文章了,刚出自己的新浪搬过来!

摇一摇做起来很简单,但是再加上iBEacon,处理起来就没有那么得心应手了

- (void)viewDidAppear:(BOOL)animated

{

  [superviewDidAppear:animated];

 // 扫描iBEacon成为第一响应

  [selfbecomeFirstResponder];

}

- (void)viewDidLoad {

// 摇一摇成为第一响应

  [[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

{

 //摇动结束

 if(event.subtype

   ==UIEventSubtypeMotionShake) {

     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

      [self.viewshowLoadingViewWithText:@"正在匹配检查报到信息"];

   DLog(@"摇动结束");

  }

}

扫描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--返回数据

 // 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];//设置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就调用

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

   _ibeaconModel=_ibeaconArr[_isSameRow];

   //查询待签到的检查订单

    [selfgetIbeaconCanBookDataWithIbeaconId:_ibeaconModel.id];

  }

 else{

    [self.viewdismissLoadingView];

//    UIAlertView *noteAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"扫描设备失败,请重新摇一摇" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

//    [noteAlertView show];

   _isRock=YES;

   _isShankCount=0;

  }

}

上一篇 下一篇

猜你喜欢

热点阅读