RQDebugTouchView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. }else{
  90. title = [NSString stringWithFormat:@"当前环境:%@", @"测试环境"];
  91. }
  92. /// 预上线
  93. UIAlertAction *switchToPrePublish = [UIAlertAction actionWithTitle:@"预上线环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  94. [RQConfigureManager setApplicationFormalSetting:YES];
  95. [RQConfigureManager setApplicationAppStoreFormalSetting:NO];
  96. [RQ_USER_MANAGER logoutUser];
  97. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  98. exit(0);
  99. });
  100. }];
  101. switchToPrePublish.textColor = RQColorFromHexString(@"#0ECCCA");
  102. /// 上线
  103. UIAlertAction *switchToPublish = [UIAlertAction actionWithTitle:@"上线环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  104. [RQConfigureManager setApplicationFormalSetting:YES];
  105. [RQConfigureManager setApplicationAppStoreFormalSetting:YES];
  106. [RQ_USER_MANAGER logoutUser];
  107. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  108. exit(0);
  109. });
  110. }];
  111. switchToPublish.textColor = RQColorFromHexString(@"#0ECCCA");
  112. /// 测试
  113. UIAlertAction *switchToDevelop = [UIAlertAction actionWithTitle:@"测试环境" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  114. [RQConfigureManager setApplicationFormalSetting:NO];
  115. [RQConfigureManager setApplicationAppStoreFormalSetting:NO];
  116. [RQ_USER_MANAGER logoutUser];
  117. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  118. exit(0);
  119. });
  120. }];
  121. switchToDevelop.textColor = RQColorFromHexString(@"#0ECCCA");
  122. /// 取消
  123. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  124. }];
  125. cancelAction.textColor = RQColorFromHexString(@"#8E929D");
  126. /// alertController
  127. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"您确定要切换环境,程序将会闪退哦\n⚡️⚡️⚡️" preferredStyle:UIAlertControllerStyleAlert];
  128. [alertController addAction:cancelAction];
  129. alertController.titleColor = RQColorFromHexString(@"#3C3E44");
  130. alertController.messageColor = RQColorFromHexString(@"#9A9A9C");
  131. if ([RQConfigureManager applicationFormalSetting]) {
  132. if ([RQConfigureManager applicationAppStoreFormalSetting]) {
  133. /// 当前 ————> 上线环境
  134. [alertController addAction:switchToPrePublish];
  135. [alertController addAction:switchToDevelop];
  136. }else{
  137. /// 当前 ————> 预上线环境
  138. [alertController addAction:switchToPublish];
  139. [alertController addAction:switchToDevelop];
  140. }
  141. }else{
  142. /// 当前 ————> 测试环境
  143. [alertController addAction:switchToPrePublish];
  144. [alertController addAction:switchToPublish];
  145. }
  146. [[RQControllerHelper currentViewController] presentViewController:alertController animated:YES completion:nil];
  147. }else if(buttonIndex == 2){
  148. /// Http/Https的切换
  149. NSString *title = [NSString stringWithFormat:@"当前方式:%@", [RQConfigureManager applicationUseHttps]?@"https":@"http"];
  150. [NSObject rq_showAlertViewWithTitle:title message:@"您确定要切换环境,程序将会闪退哦\n⚡️⚡️⚡️" confirmTitle:[RQConfigureManager applicationUseHttps]?@"http":@"https" cancelTitle:@"取消" confirmAction:^{
  151. [RQConfigureManager setApplicationUseHttps:![RQConfigureManager applicationUseHttps]];
  152. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  153. exit(0);
  154. });
  155. } cancelAction:NULL];
  156. }
  157. } otherButtonTitleArray:@[@"切换正式/测试环境",@"切换http/https"]];
  158. [actionSheet show];
  159. }
  160. #pragma mark - Override
  161. - (void)setFrame:(CGRect)frame{
  162. frame.size.width = 65;
  163. frame.size.height = 65;
  164. [super setFrame:frame];
  165. }
  166. - (void)layoutSubviews{
  167. [super layoutSubviews];
  168. CGFloat closeBtnWH = 36;
  169. self.closeBtn.frame = CGRectMake(self.frame.size.width - closeBtnWH , 0, closeBtnWH, closeBtnWH);
  170. }
  171. #pragma mark - touch move
  172. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  173. //保存触摸起始点位置
  174. CGPoint point = [[touches anyObject] locationInView:self];
  175. self.startPoint = point;
  176. //该view置于最前
  177. [[self superview] bringSubviewToFront:self];
  178. }
  179. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  180. //计算位移=当前位置-起始位置
  181. CGPoint point = [[touches anyObject] locationInView:self];
  182. float dx = point.x - self.startPoint.x;
  183. float dy = point.y - self.startPoint.y;
  184. //计算移动后的view中心点
  185. CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
  186. /* 限制用户不可将视图托出屏幕 */
  187. float halfx = CGRectGetMidX(self.bounds);
  188. //x坐标左边界
  189. newcenter.x = MAX(halfx, newcenter.x);
  190. //x坐标右边界
  191. newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
  192. //y坐标同理
  193. float halfy = CGRectGetMidY(self.bounds);
  194. newcenter.y = MAX(halfy, newcenter.y);
  195. newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
  196. /// 动画过度
  197. [UIView animateWithDuration:.25f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
  198. //移动view
  199. self.center = newcenter;
  200. } completion:^(BOOL finished) {
  201. }];
  202. }
  203. @end