BubbleView.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // BubbleView.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/30.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "BubbleView.h"
  9. #import "XenonSDK.h"
  10. @interface BubbleView () <UIGestureRecognizerDelegate>
  11. @property (nonatomic, strong) NSTimer* timer;
  12. @property (nonatomic, strong) UIPanGestureRecognizer* panGestureRecognizer;
  13. @end
  14. @implementation BubbleView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. self = [super initWithFrame:frame];
  17. self.contentView = [UIButton buttonWithType:UIButtonTypeSystem];
  18. UIImage* image = [UIImage imageNamed:@"login_mine_new" inBundle:XenonSDK.sharedSDK.bundle compatibleWithTraitCollection:nil];
  19. self.contentView.frame = self.bounds;
  20. self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  21. [self.contentView setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  22. forState:UIControlStateNormal];
  23. [self addSubview:self.contentView];
  24. return self;
  25. }
  26. - (void)startCountdonw {
  27. // Setting timer.
  28. NSTimer* timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(echo:) userInfo:nil repeats:false];
  29. [NSRunLoop.mainRunLoop addTimer:timer forMode:NSDefaultRunLoopMode];
  30. self.timer = timer;
  31. }
  32. - (void)stopCountdown {
  33. if (self.timer == nil) {
  34. return;
  35. }
  36. [self.timer invalidate];
  37. self.timer = nil;
  38. // Restore cursor state.
  39. self.transform = CGAffineTransformIdentity;
  40. self.contentView.hidden = NO;
  41. self.gradualView.hidden = YES;
  42. }
  43. - (CGPoint)estimateRange:(CGPoint)center inView:(UIView*)view {
  44. if (view == nil) {
  45. return center;
  46. }
  47. //UIEdgeInsetsInsetRect 表示在原来的rect基础上根据边缘距离内切一个rect出来,正内负外.
  48. CGRect bounds = UIEdgeInsetsInsetRect(view.bounds, UIEdgeInsetsMake(8, 8, 8, 8));;
  49. CGFloat x = center.x;
  50. CGFloat y = center.y;
  51. // Check that the distance side is closer.
  52. if (fabs((x - self.frame.size.width / 2) - CGRectGetMinX(bounds)) < fabs((x + self.frame.size.width / 2) - CGRectGetMaxX(bounds))) {
  53. x = CGRectGetMinX(bounds) + self.frame.size.width / 2; //悬浮右边
  54. } else {
  55. x = CGRectGetMaxX(bounds) - self.frame.size.width / 2;
  56. }
  57. // You can't go above or below the boundary.
  58. y = fmin(fmax(y, CGRectGetMinY(bounds) + self.frame.size.height / 2), CGRectGetMaxY(bounds) - self.frame.size.height / 2);
  59. return CGPointMake(x, y);
  60. }
  61. - (void)echo:(NSTimer*)timer {
  62. // Gets the window.
  63. if (self.window == nil) {
  64. return;
  65. }
  66. // Only need to create the view once.
  67. if (self.gradualView == nil) {
  68. // If no image is provided, ignore it.
  69. UIImage* image = [UIImage imageNamed:@"login_iv_hide_new" inBundle:XenonSDK.sharedSDK.bundle compatibleWithTraitCollection:nil];
  70. if (image == nil) {
  71. return;
  72. }
  73. // Calculate the best display effect.
  74. CGFloat height = self.bounds.size.height;
  75. CGFloat width = height * (image.size.width / image.size.height);
  76. //ps:这里显示在屏幕内的按钮宽度太小,手动处理一下. *1.1倍数
  77. // Create the view and set the same default values.
  78. UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  79. imageView.bounds = CGRectMake(0, 0, width, height);
  80. imageView.hidden = NO;
  81. imageView.userInteractionEnabled = YES;
  82. imageView.contentMode = UIViewContentModeScaleAspectFit;
  83. //
  84. [self addSubview:imageView];
  85. self.gradualView = imageView;
  86. }
  87. // Click is not allowed while hidden.
  88. self.contentView.hidden = false;
  89. self.gradualView.hidden = true;
  90. // Calculate the distance to the boundary.
  91. CGFloat offset = 0;
  92. if (fabs(CGRectGetMinX(self.frame)) < fabs(self.window.frame.size.width - CGRectGetMaxX(self.frame))) {
  93. // In left side.
  94. offset = -CGRectGetMaxX(self.frame);
  95. self.gradualView.transform = CGAffineTransformIdentity;
  96. self.gradualView.frame = CGRectMake(-CGRectGetMinX(self.frame), 0, self.gradualView.frame.size.width, self.gradualView.frame.size.height);
  97. } else {
  98. // In right side.
  99. offset = self.window.frame.size.width - CGRectGetMinX(self.frame);
  100. self.gradualView.transform = CGAffineTransformMakeScale(-1, 1);
  101. self.gradualView.frame = CGRectMake(offset - self.gradualView.frame.size.width, 0, self.gradualView.frame.size.width, self.gradualView.frame.size.height);
  102. }
  103. // Generate hidden animation.
  104. [UIView animateWithDuration:0.2 animations:^{
  105. // step 1: hide the entire button.
  106. self.transform = CGAffineTransformMakeTranslation(offset, 0);
  107. } completion:^(BOOL finished) {
  108. // step 2: Disable click events to show hidden cursors.
  109. self.contentView.hidden = true;
  110. self.gradualView.hidden = false;
  111. // step 3: Generate display animation.
  112. [UIView animateWithDuration:0.15 animations:^{
  113. self.transform = CGAffineTransformIdentity;
  114. }];
  115. }];
  116. [timer invalidate];
  117. }
  118. - (void)move:(UIPanGestureRecognizer*)sender {
  119. CGPoint location = [sender translationInView:self.superview];
  120. switch (sender.state) {
  121. case UIGestureRecognizerStateBegan:
  122. case UIGestureRecognizerStateChanged:
  123. case UIGestureRecognizerStatePossible: {
  124. // Begin drag event.
  125. self.transform = CGAffineTransformMakeTranslation(location.x, location.y);
  126. [self stopCountdown];
  127. break;
  128. }
  129. case UIGestureRecognizerStateEnded: {
  130. // End drag event.
  131. self.transform = CGAffineTransformIdentity;
  132. self.center = CGPointMake(self.center.x + location.x, self.center.y + location.y);
  133. [self startCountdonw];
  134. // Generate move animation.
  135. [UIView animateWithDuration:0.25 animations:^{
  136. self.center = [self estimateRange:self.center inView:self.superview];
  137. }];
  138. // Getting the class name directly is to reduce the number of strings in the code.
  139. [XSDataCenter setValue:@[@(self.center.x), @(self.center.y)] forKey:NSStringFromClass(self.class)];
  140. break;
  141. }
  142. default: {
  143. // End drag event in cancel.
  144. self.transform = CGAffineTransformIdentity;
  145. [self startCountdonw];
  146. break;
  147. }
  148. }
  149. }
  150. - (void)setHidden:(BOOL)hidden {
  151. [super setHidden:hidden];
  152. if (self.timer == nil || !self.gradualView.hidden) {
  153. return;
  154. }
  155. // If you are hiding, countdown is not allowed.
  156. if (hidden) {
  157. // Cancel countdown.
  158. [self.timer invalidate];
  159. } else {
  160. // Restore countdown.
  161. [self startCountdonw];
  162. }
  163. }
  164. - (void)setCenter:(CGPoint)center {
  165. [super setCenter:center];
  166. if (self.superview == nil) {
  167. return;
  168. }
  169. if (CGRectGetMinX(self.frame) < CGRectGetWidth(self.superview.bounds) / 2) {
  170. // In left side.
  171. self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  172. } else {
  173. // In right side.
  174. self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  175. }
  176. }
  177. - (void)willMoveToSuperview:(UIView *)newSuperview {
  178. // Remove or Add?
  179. if (newSuperview == nil) {
  180. // When superview remove, the pan gesture recognizer need remove.
  181. [self.panGestureRecognizer.view removeGestureRecognizer:self.panGestureRecognizer];
  182. self.panGestureRecognizer = nil;
  183. [self stopCountdown];
  184. return;
  185. }
  186. if (self.panGestureRecognizer != nil) {
  187. return ;
  188. }
  189. CGPoint center = CGPointMake(newSuperview.bounds.size.width, newSuperview.bounds.size.height / 2);
  190. // Getting the class name directly is to reduce the number of strings in the code.
  191. NSArray* data = [XSDataCenter valueForKey:NSStringFromClass(self.class)];
  192. if (data.count == 2) {
  193. center.x = [data[0] doubleValue];
  194. center.y = [data[1] doubleValue];
  195. }
  196. UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
  197. panGestureRecognizer.delegate = self;
  198. panGestureRecognizer.delaysTouchesBegan = false;
  199. panGestureRecognizer.delaysTouchesEnded = true;
  200. panGestureRecognizer.cancelsTouchesInView = true;
  201. [newSuperview addGestureRecognizer:panGestureRecognizer];
  202. self.center = [self estimateRange:center inView:newSuperview];
  203. self.panGestureRecognizer = panGestureRecognizer;
  204. [self startCountdonw];
  205. }
  206. - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  207. if (self.hidden || !self.userInteractionEnabled) {
  208. return nil;
  209. }
  210. if (CGRectContainsPoint(self.contentView.frame, point) && !self.contentView.hidden) {
  211. return self.contentView;
  212. }
  213. if (self.gradualView != nil && CGRectContainsPoint(self.gradualView.frame, point) && !self.gradualView.hidden) {
  214. return self.gradualView;
  215. }
  216. return nil;
  217. }
  218. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  219. if (self.hidden) {
  220. return NO;
  221. }
  222. CGRect bounds = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(-20, -20, -20, -20));
  223. return CGRectContainsPoint(bounds, [gestureRecognizer locationInView:self]);
  224. }
  225. @end