SearchDeviceViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // SearchDeviceViewController.m
  3. // Unity-iPhone
  4. //
  5. // Created by leon on 2021/2/4.
  6. //
  7. #import "SearchDeviceViewController.h"
  8. #import "ShoesCarouselView.h"
  9. @interface SearchDeviceViewController ()<ConnectDeviceDelegate>
  10. //
  11. @property (strong, nonatomic)ShoesCarouselView * shoesCarouselView;
  12. //button
  13. @property(nonatomic,strong)UIButton * nextButton;//
  14. @property(nonatomic,strong)UIButton * lastButton;//
  15. @property(nonatomic,strong)UIButton * cancelButton;//
  16. @property(nonatomic,strong)UIButton * connectButton;//
  17. @property (nonatomic,strong)UIButton * clearnButton;//清理缓存
  18. //留白
  19. @property(nonatomic,strong)NoDeviceTip * tipView;
  20. @end
  21. @implementation SearchDeviceViewController
  22. -(void)viewDidLoad{
  23. [super viewDidLoad];
  24. [self initUI];
  25. }
  26. #pragma mark -- create UI
  27. -(void)initUI{
  28. //创建需要的毛玻璃特效类型
  29. UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  30. //创建毛玻璃视图
  31. UIVisualEffectView *visualV = [[UIVisualEffectView alloc] initWithEffect:blur];
  32. visualV.frame = self.view.bounds;
  33. //可以通过透明度来达到模糊程序的改变.
  34. visualV.effect = nil;
  35. visualV.backgroundColor = [UIColor blackColor];
  36. visualV.alpha = 0.5;
  37. [UIView animateWithDuration:1 animations:^{
  38. visualV.effect = blur;
  39. }];
  40. [self.view addSubview:visualV];
  41. //无设备
  42. [self.view addSubview:self.tipView];
  43. //设备列表
  44. self.shoesCarouselView = [[ShoesCarouselView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-SCALEoefficient(200), SCREEN_HEIGHT-SCALEoefficient(100))];
  45. self.shoesCarouselView.centerX = self.view.centerX;
  46. self.shoesCarouselView.centerY = self.view.centerY-20;
  47. [self.view addSubview:self.shoesCarouselView];
  48. self.shoesCarouselView.delegate = self;
  49. //上一个
  50. self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
  51. self.nextButton.frame = CGRectMake(0, 0, SCALEoefficient(16), SCALEoefficient(32));
  52. self.nextButton.centerY = self.shoesCarouselView.centerY;
  53. self.nextButton.right = self.shoesCarouselView.left+10;
  54. [self.nextButton setBackgroundImage:[MYFactoryManager imageString:@"gamepop_leftarrow"] forState:UIControlStateNormal];
  55. [self.view addSubview:self.nextButton];
  56. [self.nextButton addTarget:self action:@selector(lastAction) forControlEvents:UIControlEventTouchUpInside];
  57. self.nextButton.backgroundColor = [UIColor clearColor];
  58. //下一个
  59. self.lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
  60. self.lastButton.frame = CGRectMake(0, 0, SCALEoefficient(16), SCALEoefficient(32));
  61. self.lastButton.centerY = self.shoesCarouselView.centerY;
  62. self.lastButton.left = self.shoesCarouselView.right-10;
  63. [self.lastButton setBackgroundImage:[MYFactoryManager imageString:@"gamepop_rihtarrow"] forState:UIControlStateNormal];
  64. [self.view addSubview:self.lastButton];
  65. [self.lastButton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
  66. self.lastButton.backgroundColor = [UIColor clearColor];
  67. //关闭弹窗
  68. self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  69. self.cancelButton.frame = CGRectMake(SCREEN_WIDTH-SCALEoefficient(100+20),SCALEoefficient(15), SCALEoefficient(100), SCALEoefficient(30));
  70. [self.cancelButton setTitle:@"左踮脚 · 返回" forState:UIControlStateNormal];
  71. [self.cancelButton setImage:[MYFactoryManager imageString:@"gamepop_close_notes"] forState:UIControlStateNormal];
  72. [self.cancelButton setFont: [UIFont boldSystemFontOfSize: SCALEoefficient(12)]];
  73. [self.cancelButton.titleLabel setTextColor:[UIColor whiteColor]];
  74. self.cancelButton.backgroundColor = [UIColor clearColor];
  75. [self.cancelButton setImageEdgeInsets:UIEdgeInsetsMake(0, SCALEoefficient(-10), 0, 0)];
  76. [self.cancelButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  77. [self.view addSubview:self.cancelButton];
  78. //隐藏的清理缓存的按钮
  79. self.clearnButton = [UIButton buttonWithType:UIButtonTypeCustom];
  80. self.clearnButton.frame = CGRectMake(0, SCREEN_HEIGHT-SCALEoefficient(30)-SCALEoefficient(25), SCALEoefficient(60), SCALEoefficient(25));
  81. self.clearnButton.backgroundColor = [UIColor clearColor];
  82. [self.clearnButton addTarget:self action:@selector(clearCacheWithFilePath) forControlEvents:UIControlEventTouchUpInside];
  83. [self.view addSubview:self.clearnButton];
  84. NSArray * array = @[@"gamepop_leftarrow_notes",@"gamepop_rihgtarrow_notes",@"gamepop_determine_notes"];
  85. NSArray * titleArray = @[@"左踏 · 上一个",@"右踏 · 下一个",@"右踮脚 · 确认"];
  86. for (int i =0; i<3; i++){
  87. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  88. // button.frame = CGRectMake(SCREEN_WIDTH-SCALEoefficient(62+20+20)*(4-i),SCALEoefficient(23), SCALEoefficient(62+20), SCALEoefficient(20));
  89. button.frame = CGRectMake(SCREEN_WIDTH/3*i,SCREEN_HEIGHT-SCALEoefficient(30), SCREEN_WIDTH/3, SCALEoefficient(30));
  90. [button setTitle:titleArray[i] forState:UIControlStateNormal];
  91. [button setImage:[MYFactoryManager imageString:array[i]] forState:UIControlStateNormal];
  92. [button setFont: [UIFont boldSystemFontOfSize: SCALEoefficient(10)]];
  93. [button.titleLabel setTextColor:[UIColor whiteColor]];
  94. // button.tag = i;
  95. button.backgroundColor = [UIColor blackColor];
  96. [button setImageEdgeInsets:UIEdgeInsetsMake(0, SCALEoefficient(-10), 0, 0)];
  97. // [searchButton addTarget:self action:@selector(mapBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  98. [self.view addSubview:button];
  99. }
  100. }
  101. #pragma mark -------------- 外部事件
  102. //刷新蓝牙数据
  103. -(void)reloadData{
  104. NSMutableArray * dataArr = [NSMutableArray new];
  105. for (int i = 0; i<self.deviceArray.count; i++){
  106. ShoesPeripheralMolde *model = [[ShoesPeripheralMolde alloc]init];
  107. model.numberId = i;
  108. model.peripheral = self.deviceArray[i];
  109. // NSLog(@"model.peripheral === >> %@",model.peripheral.name);
  110. [dataArr addObject:model];
  111. }
  112. [self.shoesCarouselView setArrayData:dataArr];
  113. if (dataArr.count==0){
  114. self.tipView.hidden = NO;
  115. }else{
  116. self.tipView.hidden = YES;
  117. }
  118. }
  119. //蓝牙传过来的脚步交互动作
  120. -(void)getInteraction:(int)interaction{
  121. if (interaction == 0){//原地 -- 复位
  122. }else if(interaction == 3){//左踩 -- 选中上一个设备
  123. [self lastAction];
  124. }else if(interaction == 4){//右踩 -- 选中下一个设备
  125. [self nextAction];
  126. }else if(interaction == 6){//左垫脚 -- 关闭弹窗
  127. [self closeAction];
  128. }else if(interaction == 5){//右垫脚 -- 链接设备
  129. if (self.connectDeviceBlock){
  130. self.connectDeviceBlock(self.currentPeripheral);
  131. }
  132. [self closeAction];
  133. }
  134. }
  135. ////点击搜索按钮(暂留)
  136. //-(void)searchAction{
  137. //
  138. // self.tipView.hidden = NO;
  139. // if(self.searchDeviceTask){
  140. // [BTDataInstance initCBCentralManager];
  141. // }
  142. //
  143. //}
  144. #pragma mark -------------- 点击界面交互Action
  145. //选中上一个设备
  146. - (void)lastAction{
  147. [_shoesCarouselView yw_gotoNext:-1];
  148. }
  149. //选中下一个设备
  150. -(void)nextAction{
  151. [_shoesCarouselView yw_gotoNext:1];
  152. }
  153. //确认选中的设备 开始蓝牙连接
  154. -(void)selectedDeviceAction{
  155. if (self.connectDeviceBlock){
  156. self.connectDeviceBlock(self.currentPeripheral);
  157. }
  158. [self closeAction];
  159. }
  160. //关闭弹窗
  161. - (void)closeAction{
  162. if (self.closeBlock){
  163. self.closeBlock();
  164. }
  165. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  166. [UIView animateWithDuration:0.25 animations:^{
  167. self.view.alpha = 0;
  168. [self.view removeFromSuperview];
  169. }completion:^(BOOL finished){
  170. [self.view removeFromSuperview];
  171. }];
  172. });
  173. }
  174. //清理缓存
  175. -(void)clearCacheWithFilePath{
  176. [DebugViewInstance clearCacheWithFilePath];
  177. }
  178. #pragma mark -------------- shoesCarouseView delegate
  179. -(void)setCurrentperipheral:(CBPeripheral*)peripheral{
  180. self.currentPeripheral = peripheral;
  181. }
  182. //来自shoesCarouseView被选中的当前蓝牙
  183. -(void)connectDevice:(CBPeripheral*)peripheral{
  184. self.currentPeripheral = peripheral;
  185. //链接蓝牙
  186. if (self.connectDeviceBlock){
  187. self.connectDeviceBlock(self.currentPeripheral);
  188. }
  189. [self closeAction];
  190. }
  191. #pragma mark -------------- lazy
  192. -(NSMutableArray*)deviceArray{
  193. if (!_deviceArray){
  194. _deviceArray = [NSMutableArray new];
  195. }
  196. return _deviceArray;
  197. }
  198. -(NoDeviceTip*)tipView{
  199. if (!_tipView){
  200. _tipView = [[NoDeviceTip alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, SCREEN_HEIGHT/2-100, 100, 150)];
  201. _tipView.hidden = NO;
  202. // _tipView.backgroundColor = [UIColor redColor];
  203. }
  204. return _tipView;
  205. }
  206. @end