QuickConfigurationView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // QuickConfigurationView.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/15.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "QuickConfigurationView.h"
  9. #import <Masonry/Masonry.h>
  10. #import "NSString+Category.h"
  11. #import "Header.h"
  12. @implementation QuickConfigurationView
  13. {
  14. NSTimer *_myTimer; // 动画计时器
  15. int _myATime; // 动画进行时间
  16. BOOL _configing; // 是否正在配置
  17. }
  18. -(instancetype)initWithFrame:(CGRect)frame{
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. _configing = NO;
  22. self.backgroundColor = [UIColor whiteColor];
  23. [self addSubview:self.wifiLab];
  24. [self addSubview:self.wifiTF];
  25. [self addSubview:self.passwordLab];
  26. [self addSubview:self.passwordTF];
  27. [self addSubview:self.tipsLab];
  28. [self addSubview:self.startBtn];
  29. //控件布局
  30. [self configSubView];
  31. }
  32. return self;
  33. }
  34. #pragma mark-控件布局
  35. -(void)configSubView{
  36. [self.wifiLab mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.equalTo(@20);
  38. make.top.equalTo(@100);
  39. make.width.equalTo(@80);
  40. make.height.equalTo(@30);
  41. }];
  42. [self.wifiTF mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.left.equalTo(self.wifiLab.mas_right).offset(5);
  44. make.centerY.equalTo(self.wifiLab.mas_centerY);
  45. make.right.equalTo(self.mas_right).offset(-20);
  46. make.height.equalTo(@30);
  47. }];
  48. [self.passwordLab mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.left.equalTo(@20);
  50. make.top.equalTo(self.wifiLab.mas_bottom).offset(5);
  51. make.width.equalTo(@80);
  52. make.height.equalTo(@30);
  53. }];
  54. [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.left.equalTo(self.passwordLab.mas_right).offset(5);
  56. make.centerY.equalTo(self.passwordLab.mas_centerY);
  57. make.right.equalTo(self.mas_right).offset(-20);
  58. make.height.equalTo(@30);
  59. }];
  60. [self.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.top.equalTo(self.passwordLab.mas_bottom).offset(30);
  62. make.left.equalTo(@20);
  63. make.right.equalTo(self.mas_right).offset(-20);
  64. make.height.equalTo(@100);
  65. }];
  66. [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.top.equalTo(self.tipsLab.mas_bottom).offset(50);
  68. make.width.equalTo(self).multipliedBy(0.9);
  69. make.height.equalTo(@45);
  70. make.centerX.equalTo(self);
  71. }];
  72. }
  73. #pragma mark - touch event
  74. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  75. [self endEditing:YES];
  76. }
  77. #pragma mark 开始快速配置
  78. -(void)startConfigurationClicked{
  79. [self endEditing:YES];
  80. if (self.passwordTF.text.length == 0 || self.wifiTF.text.length == 0) {
  81. [SVProgressHUD showErrorWithStatus:TS("EE_ACCOUNT_PARMA_ABNORMAL")];
  82. return;
  83. }
  84. [self createPlayView];
  85. [self.radarView startSeek];
  86. _myATime = 120;
  87. _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myTimeCount:) userInfo:nil repeats:YES];
  88. if (self.startConfig) {
  89. self.startConfig(self.wifiTF.text, self.passwordTF.text);
  90. }
  91. }
  92. #pragma mark 停止快速配置
  93. -(void)stopConfigurationClicked{
  94. if (_myTimer != nil) {
  95. [_myTimer invalidate];
  96. }
  97. [self cancelAPAction];
  98. }
  99. #pragma mark 快速配置成功,停止计时
  100. -(void)stopTiming{
  101. if (_myTimer != nil) {
  102. [_myTimer invalidate];
  103. }
  104. _myATime = 0;
  105. self.countLab.text = @"";
  106. _configing = NO;
  107. }
  108. #pragma mark 配置计时
  109. -(void)myTimeCount:(NSTimer*)timer{
  110. _myATime = _myATime - 1;
  111. // 判断是否正在配置 倒计时大于零 说明在配置
  112. if (_myATime >= 0) {
  113. _configing = YES;
  114. }else{
  115. _configing = NO;
  116. }
  117. if (_myATime < 0) {
  118. // 快速配置失败
  119. [timer invalidate];
  120. [self cancelAPAction];
  121. }
  122. self.countLab.text = [NSString stringWithFormat:@"%i",_myATime];
  123. }
  124. #pragma mark 取消快速配置
  125. -(void)cancelAPAction{
  126. _myATime = 0;
  127. self.countLab.text = @"";
  128. self.radarView.hidden = YES;
  129. _configing = NO;
  130. if (self.stopConfig) {
  131. self.stopConfig();
  132. }
  133. }
  134. #pragma mark 点击配网成功的设备
  135. -(void)btnDeviceAction:(UIButton *)sender{
  136. [self cancelAPAction];
  137. if(self.addDevice){
  138. self.addDevice();
  139. }
  140. }
  141. #pragma mark - 初始化数据
  142. -(void)createPlayView{
  143. if (self.radarView == nil) {
  144. self.radarView = [[MyRadarView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight - NavHeight)];
  145. self.radarView.backgroundColor = [UIColor whiteColor];
  146. [self addSubview:self.radarView];
  147. UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
  148. cancel.center = CGPointMake(self.radarView.frame.size.width * 0.5, self.radarView.frame.size.height - 70);
  149. [cancel setTitleColor:[UIColor colorWithRed:100/255.0 green:164/255.0 blue:230/255.0 alpha:1] forState:UIControlStateNormal];
  150. cancel.backgroundColor = [UIColor clearColor];
  151. [cancel setTitle:TS("Cancel") forState:UIControlStateNormal];
  152. cancel.tag = 101010;
  153. [cancel addTarget:self action:@selector(stopConfigurationClicked) forControlEvents:UIControlEventTouchUpInside];
  154. [self.radarView addSubview:cancel];
  155. self.configTipsLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 35)];
  156. self.configTipsLab.textColor = [UIColor whiteColor];
  157. self.configTipsLab.textAlignment = NSTextAlignmentCenter;
  158. self.configTipsLab.center = CGPointMake(ScreenWidth * 0.5, self.radarView.frame.size.height - 90);
  159. self.configTipsLab.backgroundColor = [UIColor colorWithRed:54/255.0 green:54/255.0 blue:54/255.0 alpha:1];
  160. [self.radarView addSubview:self.configTipsLab];
  161. if ([NSString checkSSID:[NSString getCurrent_SSID]]) {
  162. cancel.hidden = YES;
  163. self.configTipsLab.hidden = NO;
  164. }else{
  165. cancel.hidden = NO;
  166. self.configTipsLab.hidden = YES;
  167. }
  168. UIImageView *phone = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth * 0.125 , ScreenWidth * 0.125)];
  169. phone.center = CGPointMake(self.radarView.frame.size.width * 0.5, self.radarView.frame.size.height * 0.5);
  170. [self.radarView addSubview:phone];
  171. self.countLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
  172. self.countLab.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:20];
  173. self.countLab.backgroundColor = [UIColor clearColor];
  174. self.countLab.textAlignment = NSTextAlignmentCenter;
  175. self.countLab.textColor = [UIColor whiteColor];
  176. self.countLab.center = phone.center;
  177. [self.radarView addSubview:self.countLab];
  178. }
  179. if (self.btnArray == nil) {
  180. self.btnArray = [[NSMutableArray alloc] initWithCapacity:0];
  181. }
  182. for (int i = 0; i < self.btnArray.count; i ++) {
  183. [[self.btnArray objectAtIndex:i] removeFromSuperview];
  184. }
  185. [self.btnArray removeAllObjects];
  186. self.radarView.hidden = NO;
  187. //有设备则显示设备图标按钮
  188. for (int i = 0; i < self.deviceArray.count; i ++) {
  189. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  190. btn.tag = i;
  191. CGPoint btnPoint = CGPointMake(-20, -20);
  192. double sum;
  193. for (int i = 0; i < 99999999; i ++) {
  194. btnPoint = [self getPoint];
  195. double x = ((btnPoint.x - ScreenWidth * 0.5)*(btnPoint.x - ScreenWidth * 0.5));
  196. double y = (btnPoint.y - self.radarView.frame.size.height * 0.5)*(btnPoint.y - self.radarView.frame.size.height * 0.5);
  197. sum = x + y;
  198. if ((sqrt(sum) < ScreenWidth * 0.375)) {
  199. break;
  200. }
  201. }
  202. btn.center = btnPoint;
  203. [btn setBackgroundImage:[UIImage imageNamed:@"phone2.png"] forState:UIControlStateNormal];
  204. [btn addTarget:self action:@selector(btnDeviceAction:) forControlEvents:UIControlEventTouchUpInside];
  205. [self.btnArray addObject:btn];
  206. [self.radarView addSubview:btn];
  207. }
  208. }
  209. //随机获取坐标
  210. -(CGPoint)getPoint{
  211. CGPoint point;
  212. CGFloat mX = arc4random()%(int)(ScreenWidth * 0.875) + ScreenWidth * 0.125;
  213. CGFloat mY = arc4random()%(int)(ScreenHeight - 64 - ScreenWidth * 0.125) + ScreenWidth * 0.125;
  214. point.x = mX;
  215. point.y = mY;
  216. return point;
  217. }
  218. #pragma mark - lazyload
  219. -(UILabel *)wifiLab{
  220. if (!_wifiLab) {
  221. _wifiLab = [[UILabel alloc] init];
  222. _wifiLab.text = TS("WIFI:");
  223. }
  224. return _wifiLab;
  225. }
  226. -(UILabel *)passwordLab{
  227. if (!_passwordLab) {
  228. _passwordLab = [[UILabel alloc] init];
  229. _passwordLab.text = TS("Password2");
  230. }
  231. return _passwordLab;
  232. }
  233. -(UITextField *)wifiTF{
  234. if (!_wifiTF) {
  235. _wifiTF = [[UITextField alloc] init];
  236. _wifiTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:TS("") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
  237. }
  238. return _wifiTF;
  239. }
  240. -(UITextField *)passwordTF{
  241. if (!_passwordTF) {
  242. _passwordTF = [[UITextField alloc] init];
  243. _passwordTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:TS("Enter_WIFIPassword") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
  244. }
  245. return _passwordTF;
  246. }
  247. -(UILabel *)tipsLab{
  248. if (!_tipsLab) {
  249. _tipsLab = [[UILabel alloc] init];
  250. _tipsLab.numberOfLines = 0;
  251. _tipsLab.font = [UIFont systemFontOfSize:14];
  252. _tipsLab.text = TS("wifi_config_tip");
  253. }
  254. return _tipsLab;
  255. }
  256. -(UIButton *)startBtn{
  257. if (!_startBtn) {
  258. _startBtn = [[UIButton alloc] init];
  259. [_startBtn setTitle:TS("OK") forState:UIControlStateNormal];
  260. [_startBtn setBackgroundColor:GlobalMainColor];
  261. [_startBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
  262. [_startBtn addTarget:self action:@selector(startConfigurationClicked) forControlEvents:UIControlEventTouchUpInside];
  263. }
  264. return _startBtn;
  265. }
  266. -(NSMutableArray *)deviceArray{
  267. if (!_deviceArray) {
  268. _deviceArray = [[NSMutableArray alloc] initWithCapacity:0];
  269. }
  270. return _deviceArray;
  271. }
  272. @end