RQDebugTouchView.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // RQDebugTouchView.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/23.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQDebugTouchView.h"
  9. #import "RQConfigureManager.h"
  10. #import "RQControllerHelper.h"
  11. /// 显示状态的key
  12. static NSString * const RQDebugTouchViewStatusKey = @"RQDebugTouchViewStatusKey";
  13. @interface RQDebugTouchView ()
  14. /// closeBtn
  15. @property (nonatomic, readwrite, weak) UIButton *closeBtn;
  16. /// 开始点
  17. @property (nonatomic, readwrite, assign) CGPoint startPoint;
  18. /// 是否在动画
  19. @property (nonatomic, readwrite, assign , getter = isAnimated) BOOL animated;
  20. @end
  21. @implementation RQDebugTouchView
  22. @def_singleton(RQDebugTouchView);
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. self.image = RQImageNamed(@"assistivetouch");
  27. self.userInteractionEnabled = YES;
  28. /// 添加tap手势
  29. [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapAction:)]];
  30. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  31. [window addSubview:self];
  32. self.rq_x = [UIScreen mainScreen].bounds.size.width - self.rq_width - 20;
  33. self.rq_y = 84;
  34. self.hidden = [self isHide];
  35. }
  36. return self;
  37. }
  38. - (void)setHide:(BOOL)hide {
  39. if (self.isAnimated) return;
  40. [[NSUserDefaults standardUserDefaults] setBool:hide forKey:RQDebugTouchViewStatusKey];
  41. [[NSUserDefaults standardUserDefaults] synchronize];
  42. hide?[self close]:[self open];
  43. }
  44. - (BOOL)isHide{
  45. BOOL temp = [[NSUserDefaults standardUserDefaults] boolForKey:RQDebugTouchViewStatusKey];
  46. NSLog(@"++++ Touch View Is Hide %d ++++" , temp);
  47. return temp;
  48. }
  49. - (void)open{
  50. /// 动画开始,禁止交互
  51. self.userInteractionEnabled = NO;
  52. self.animated = YES;
  53. self.hidden = NO;
  54. self.transform = CGAffineTransformIdentity;
  55. self.transform = CGAffineTransformMakeScale(.2, .2);
  56. [UIView animateWithDuration:.25f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
  57. self.transform = CGAffineTransformMakeScale(1.0, 1.0);
  58. } completion:^(BOOL finished) {
  59. self.transform = CGAffineTransformIdentity;
  60. /// 动画结束,允许交互
  61. self.userInteractionEnabled = YES;
  62. self.animated = NO;
  63. }];
  64. }
  65. - (void)close{
  66. /// 动画开始,禁止交互
  67. self.animated = YES;
  68. self.userInteractionEnabled = NO;
  69. [UIView animateWithDuration:.25f animations:^{
  70. self.transform = CGAffineTransformMakeScale(.2, .2);
  71. } completion:^(BOOL finished) {
  72. self.transform = CGAffineTransformIdentity;
  73. self.hidden = YES;
  74. /// 动画结束,允许交互
  75. self.userInteractionEnabled = YES;
  76. self.animated = NO;
  77. }];
  78. }
  79. #pragma mark - Action
  80. - (void)_tapAction:(UITapGestureRecognizer *)tapGr{
  81. LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:nil cancelButtonTitle:@"取消" didDismiss:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
  82. if (buttonIndex == 0) return ;
  83. if (buttonIndex == 1) {
  84. /// 环境的切换
  85. /// 主题
  86. NSString *title = nil;
  87. if ([RQConfigureManager applicationFormalSetting]) {
  88. // title = [RQConfigureManager applicationAppStoreFormalSetting]? [NSString stringWithFormat:@"当前环境:%@", @"上线环境"]: [NSString stringWithFormat:@"当前环境:%@", @"预上线环境"];
  89. title = [NSString stringWithFormat:@"当前环境:%@", @"上线环境"];
  90. }else{
  91. title = [NSString stringWithFormat:@"当前环境:%@", @"测试环境"];
  92. }
  93. /// 预上线
  94. // UIAlertAction *switchToPrePublish = [UIAlertAction actionWithTitle:@"预上线环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  95. // [RQConfigureManager setApplicationFormalSetting:YES];
  96. // [RQConfigureManager setApplicationAppStoreFormalSetting:NO];
  97. // [RQ_USER_MANAGER logoutUser];
  98. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  99. // exit(0);
  100. // });
  101. // }];
  102. // switchToPrePublish.textColor = RQColorFromHexString(@"#0ECCCA");
  103. /// 上线
  104. UIAlertAction *switchToPublish = [UIAlertAction actionWithTitle:@"上线环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  105. [RQConfigureManager setApplicationFormalSetting:YES];
  106. // [RQConfigureManager setApplicationAppStoreFormalSetting:YES];
  107. [RQ_USER_MANAGER logoutUser];
  108. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  109. exit(0);
  110. });
  111. }];
  112. switchToPublish.textColor = RQColorFromHexString(@"#0ECCCA");
  113. /// 测试
  114. UIAlertAction *switchToDevelop = [UIAlertAction actionWithTitle:@"测试环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  115. [RQConfigureManager setApplicationFormalSetting:NO];
  116. // [RQConfigureManager setApplicationAppStoreFormalSetting:NO];
  117. [RQ_USER_MANAGER logoutUser];
  118. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  119. exit(0);
  120. });
  121. }];
  122. switchToDevelop.textColor = RQColorFromHexString(@"#0ECCCA");
  123. /// 取消
  124. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  125. }];
  126. cancelAction.textColor = RQColorFromHexString(@"#8E929D");
  127. /// alertController
  128. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"您确定要切换环境,程序将会闪退哦\n⚡️⚡️⚡️" preferredStyle:UIAlertControllerStyleAlert];
  129. [alertController addAction:cancelAction];
  130. alertController.titleColor = RQColorFromHexString(@"#3C3E44");
  131. alertController.messageColor = RQColorFromHexString(@"#9A9A9C");
  132. if ([RQConfigureManager applicationFormalSetting]) {
  133. [alertController addAction:switchToDevelop];
  134. // if ([RQConfigureManager applicationAppStoreFormalSetting]) {
  135. // /// 当前 ————> 上线环境
  136. // [alertController addAction:switchToPrePublish];
  137. // [alertController addAction:switchToDevelop];
  138. // }else{
  139. // /// 当前 ————> 预上线环境
  140. // [alertController addAction:switchToPublish];
  141. // [alertController addAction:switchToDevelop];
  142. // }
  143. }else{
  144. /// 当前 ————> 测试环境
  145. // [alertController addAction:switchToPrePublish];
  146. [alertController addAction:switchToPublish];
  147. }
  148. [[RQControllerHelper currentViewController] presentViewController:alertController animated:YES completion:nil];
  149. }else if(buttonIndex == 2){
  150. /// Http/Https的切换
  151. NSString *title = [NSString stringWithFormat:@"当前方式:%@", [RQConfigureManager applicationUseHttps]?@"https":@"http"];
  152. [NSObject rq_showAlertViewWithTitle:title message:@"您确定要切换环境,程序将会闪退哦\n⚡️⚡️⚡️" confirmTitle:[RQConfigureManager applicationUseHttps]?@"http":@"https" cancelTitle:@"取消" confirmAction:^{
  153. [RQConfigureManager setApplicationUseHttps:![RQConfigureManager applicationUseHttps]];
  154. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  155. exit(0);
  156. });
  157. } cancelAction:NULL];
  158. }
  159. } otherButtonTitleArray:@[@"切换正式/测试环境"/*,@"切换http/https"*/]];
  160. [actionSheet show];
  161. }
  162. #pragma mark - Override
  163. - (void)setFrame:(CGRect)frame{
  164. frame.size.width = 65;
  165. frame.size.height = 65;
  166. [super setFrame:frame];
  167. }
  168. - (void)layoutSubviews{
  169. [super layoutSubviews];
  170. CGFloat closeBtnWH = 36;
  171. self.closeBtn.frame = CGRectMake(self.frame.size.width - closeBtnWH , 0, closeBtnWH, closeBtnWH);
  172. }
  173. #pragma mark - touch move
  174. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  175. //保存触摸起始点位置
  176. CGPoint point = [[touches anyObject] locationInView:self];
  177. self.startPoint = point;
  178. //该view置于最前
  179. [[self superview] bringSubviewToFront:self];
  180. }
  181. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  182. //计算位移=当前位置-起始位置
  183. CGPoint point = [[touches anyObject] locationInView:self];
  184. float dx = point.x - self.startPoint.x;
  185. float dy = point.y - self.startPoint.y;
  186. //计算移动后的view中心点
  187. CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
  188. /* 限制用户不可将视图托出屏幕 */
  189. float halfx = CGRectGetMidX(self.bounds);
  190. //x坐标左边界
  191. newcenter.x = MAX(halfx, newcenter.x);
  192. //x坐标右边界
  193. newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
  194. //y坐标同理
  195. float halfy = CGRectGetMidY(self.bounds);
  196. newcenter.y = MAX(halfy, newcenter.y);
  197. newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
  198. /// 动画过度
  199. [UIView animateWithDuration:.25f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
  200. //移动view
  201. self.center = newcenter;
  202. } completion:^(BOOL finished) {
  203. }];
  204. }
  205. @end