BubbleView.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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" 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. CGRect bounds = UIEdgeInsetsInsetRect(view.bounds, UIEdgeInsetsMake(8, 8, 8, 8));;
  48. CGFloat x = center.x;
  49. CGFloat y = center.y;
  50. // Check that the distance side is closer.
  51. if (fabs((x - self.frame.size.width / 2) - CGRectGetMinX(bounds)) < fabs((x + self.frame.size.width / 2) - CGRectGetMaxX(bounds))) {
  52. x = CGRectGetMinX(bounds) + self.frame.size.width / 2;
  53. } else {
  54. x = CGRectGetMaxX(bounds) - self.frame.size.width / 2;
  55. }
  56. // You can't go above or below the boundary.
  57. y = fmin(fmax(y, CGRectGetMinY(bounds) + self.frame.size.height / 2), CGRectGetMaxY(bounds) - self.frame.size.height / 2);
  58. return CGPointMake(x, y);
  59. }
  60. - (void)echo:(NSTimer*)timer {
  61. // Gets the window.
  62. if (self.window == nil) {
  63. return;
  64. }
  65. // Only need to create the view once.
  66. if (self.gradualView == nil) {
  67. // If no image is provided, ignore it.
  68. UIImage* image = [UIImage imageNamed:@"login_iv_hide" inBundle:XenonSDK.sharedSDK.bundle compatibleWithTraitCollection:nil];
  69. if (image == nil) {
  70. return;
  71. }
  72. // Calculate the best display effect.
  73. CGFloat height = self.bounds.size.height;
  74. CGFloat width = height * (image.size.width / image.size.height);
  75. // Create the view and set the same default values.
  76. UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  77. imageView.bounds = CGRectMake(0, 0, width, height);
  78. imageView.hidden = NO;
  79. imageView.userInteractionEnabled = NO;
  80. imageView.contentMode = UIViewContentModeScaleAspectFit;
  81. [self addSubview:imageView];
  82. self.gradualView = imageView;
  83. }
  84. // Click is not allowed while hidden.
  85. self.contentView.hidden = false;
  86. self.gradualView.hidden = true;
  87. // Calculate the distance to the boundary.
  88. CGFloat offset = 0;
  89. if (fabs(CGRectGetMinX(self.frame)) < fabs(self.window.frame.size.width - CGRectGetMaxX(self.frame))) {
  90. // In left side.
  91. offset = -CGRectGetMaxX(self.frame);
  92. self.gradualView.transform = CGAffineTransformIdentity;
  93. self.gradualView.frame = CGRectMake(-CGRectGetMinX(self.frame), 0, self.gradualView.frame.size.width, self.gradualView.frame.size.height);
  94. } else {
  95. // In right side.
  96. offset = self.window.frame.size.width - CGRectGetMinX(self.frame);
  97. self.gradualView.transform = CGAffineTransformMakeScale(-1, 1);
  98. self.gradualView.frame = CGRectMake(offset - self.gradualView.frame.size.width, 0, self.gradualView.frame.size.width, self.gradualView.frame.size.height);
  99. }
  100. // Generate hidden animation.
  101. [UIView animateWithDuration:0.2 animations:^{
  102. // step 1: hide the entire button.
  103. self.transform = CGAffineTransformMakeTranslation(offset, 0);
  104. } completion:^(BOOL finished) {
  105. // step 2: Disable click events to show hidden cursors.
  106. self.contentView.hidden = true;
  107. self.gradualView.hidden = false;
  108. // step 3: Generate display animation.
  109. [UIView animateWithDuration:0.15 animations:^{
  110. self.transform = CGAffineTransformIdentity;
  111. }];
  112. }];
  113. [timer invalidate];
  114. }
  115. - (void)move:(UIPanGestureRecognizer*)sender {
  116. CGPoint location = [sender translationInView:self.superview];
  117. switch (sender.state) {
  118. case UIGestureRecognizerStateBegan:
  119. case UIGestureRecognizerStateChanged:
  120. case UIGestureRecognizerStatePossible: {
  121. // Begin drag event.
  122. self.transform = CGAffineTransformMakeTranslation(location.x, location.y);
  123. [self stopCountdown];
  124. break;
  125. }
  126. case UIGestureRecognizerStateEnded: {
  127. // End drag event.
  128. self.transform = CGAffineTransformIdentity;
  129. self.center = CGPointMake(self.center.x + location.x, self.center.y + location.y);
  130. [self startCountdonw];
  131. // Generate move animation.
  132. [UIView animateWithDuration:0.25 animations:^{
  133. self.center = [self estimateRange:self.center inView:self.superview];
  134. }];
  135. // Getting the class name directly is to reduce the number of strings in the code.
  136. [XSDataCenter setValue:@[@(self.center.x), @(self.center.y)] forKey:NSStringFromClass(self.class)];
  137. break;
  138. }
  139. default: {
  140. // End drag event in cancel.
  141. self.transform = CGAffineTransformIdentity;
  142. [self startCountdonw];
  143. break;
  144. }
  145. }
  146. }
  147. - (void)setHidden:(BOOL)hidden {
  148. [super setHidden:hidden];
  149. if (self.timer == nil || !self.gradualView.hidden) {
  150. return;
  151. }
  152. // If you are hiding, countdown is not allowed.
  153. if (hidden) {
  154. // Cancel countdown.
  155. [self.timer invalidate];
  156. } else {
  157. // Restore countdown.
  158. [self startCountdonw];
  159. }
  160. }
  161. - (void)setCenter:(CGPoint)center {
  162. [super setCenter:center];
  163. if (self.superview == nil) {
  164. return;
  165. }
  166. if (CGRectGetMinX(self.frame) < CGRectGetWidth(self.superview.bounds) / 2) {
  167. // In left side.
  168. self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  169. } else {
  170. // In right side.
  171. self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  172. }
  173. }
  174. - (void)willMoveToSuperview:(UIView *)newSuperview {
  175. // Remove or Add?
  176. if (newSuperview == nil) {
  177. // When superview remove, the pan gesture recognizer need remove.
  178. [self.panGestureRecognizer.view removeGestureRecognizer:self.panGestureRecognizer];
  179. self.panGestureRecognizer = nil;
  180. [self stopCountdown];
  181. return;
  182. }
  183. if (self.panGestureRecognizer != nil) {
  184. return ;
  185. }
  186. CGPoint center = CGPointMake(newSuperview.bounds.size.width, newSuperview.bounds.size.height / 2);
  187. // Getting the class name directly is to reduce the number of strings in the code.
  188. NSArray* data = [XSDataCenter valueForKey:NSStringFromClass(self.class)];
  189. if (data.count == 2) {
  190. center.x = [data[0] doubleValue];
  191. center.y = [data[1] doubleValue];
  192. }
  193. UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
  194. panGestureRecognizer.delegate = self;
  195. panGestureRecognizer.delaysTouchesBegan = false;
  196. panGestureRecognizer.delaysTouchesEnded = true;
  197. panGestureRecognizer.cancelsTouchesInView = true;
  198. [newSuperview addGestureRecognizer:panGestureRecognizer];
  199. self.center = [self estimateRange:center inView:newSuperview];
  200. self.panGestureRecognizer = panGestureRecognizer;
  201. [self startCountdonw];
  202. }
  203. - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  204. if (self.hidden || !self.userInteractionEnabled) {
  205. return nil;
  206. }
  207. if (CGRectContainsPoint(self.contentView.frame, point) && !self.contentView.hidden) {
  208. return self.contentView;
  209. }
  210. if (self.gradualView != nil && CGRectContainsPoint(self.gradualView.frame, point) && !self.gradualView.hidden) {
  211. return self.gradualView;
  212. }
  213. return nil;
  214. }
  215. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  216. if (self.hidden) {
  217. return NO;
  218. }
  219. CGRect bounds = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(-20, -20, -20, -20));
  220. return CGRectContainsPoint(bounds, [gestureRecognizer locationInView:self]);
  221. }
  222. @end