TMSmallRoomView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // TMSmallRoomView.m
  3. // TamuVoice
  4. //
  5. // Created by tamu on 2021/9/20.
  6. //
  7. #import "TMSmallRoomView.h"
  8. @implementation TMSmallRoomView
  9. static dispatch_once_t onceToken;
  10. static TMSmallRoomView *_roomView;
  11. + (instancetype)sharedInstance{
  12. dispatch_once(&onceToken, ^{
  13. _roomView = [[TMSmallRoomView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70-15, NavigationHeight + 60, 70, 92)];
  14. _roomView.hidden = YES;
  15. UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
  16. NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
  17. for (UIWindow *window in frontToBackWindows) {
  18. BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
  19. BOOL windowIsVisible = !window.hidden && window.alpha > 0;
  20. BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;
  21. if(windowOnMainScreen && windowIsVisible && windowLevelNormal) {
  22. currentWindow = window;
  23. break;
  24. }
  25. }
  26. [currentWindow addSubview:_roomView];
  27. _roomView.layer.cornerRadius = 10;
  28. _roomView.layer.masksToBounds = YES;
  29. });
  30. return _roomView;
  31. }
  32. #pragma mark -
  33. #pragma mark --- init frame
  34. -(instancetype)initWithFrame:(CGRect)frame
  35. {
  36. self = [super initWithFrame:frame];
  37. if (self) {
  38. /** 初始化*/
  39. [self initContentView];
  40. /** RAC*/
  41. [self initRAC];
  42. }
  43. return self ;
  44. }
  45. #pragma mark -
  46. #pragma mark --- 视图初始化
  47. - (void)initContentView {
  48. UIImageView * iconImageView = [[UIImageView alloc] init];
  49. iconImageView.image = [UIImage imageNamed:@"audioChatSmallphoneicon"];
  50. [self addSubview:iconImageView];
  51. [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.centerX.mas_equalTo(self);
  53. make.width.height.mas_equalTo(40);
  54. make.top.mas_equalTo(15);
  55. }];
  56. self.backgroundColor = [UIColor whiteColor];
  57. [self addSubview:self.timeLabel];
  58. [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.centerX.mas_equalTo(self);
  60. make.bottom.mas_equalTo(-15);
  61. }];
  62. }
  63. #pragma mark --- 初始化RAC
  64. - (void)initRAC {
  65. @weakify(self)
  66. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] init];
  67. [tap addActionBlock:^(id _Nonnull sender) {
  68. @strongify(self)
  69. if (self.enterClickBlock) {
  70. self.enterClickBlock();
  71. }
  72. [self dismiss];
  73. }];
  74. [self addGestureRecognizer:tap];
  75. //添加手势
  76. UIPanGestureRecognizer * panRcognize = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  77. [panRcognize setMinimumNumberOfTouches:1];
  78. [panRcognize setEnabled:YES];
  79. [panRcognize delaysTouchesEnded];
  80. [panRcognize cancelsTouchesInView];
  81. [self addGestureRecognizer:panRcognize];
  82. }
  83. //- (void)updateUIWithData:(TMRoomModel *)model{
  84. //
  85. // if (![model isKindOfClass:[TMRoomModel class]]) {
  86. // return;
  87. // }
  88. //
  89. // [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.homeHead] placeholderImage:kPlaceholderHeadImage()];
  90. // self.titleLabel.text = model.homeName;
  91. // self.idLabel.text = [NSString stringWithFormat:@"ID:%ld",(long)model.homeNumber];
  92. //
  93. //}
  94. - (void)dismiss{
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. self.hidden = YES;
  97. onceToken = 0;
  98. _roomView = nil;
  99. AppDelegate.sharedAppDelegate.hasSmallAudioVideoView = NO;
  100. });
  101. }
  102. - (void)show{
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. self.hidden = NO;
  105. });
  106. }
  107. - (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer {
  108. //移动状态
  109. UIGestureRecognizerState recState = recognizer.state;
  110. switch (recState) {
  111. case UIGestureRecognizerStateBegan:
  112. break;
  113. case UIGestureRecognizerStateChanged:
  114. {
  115. CGPoint translation = [recognizer translationInView:self.window];
  116. recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
  117. }
  118. break;
  119. case UIGestureRecognizerStateEnded:
  120. {
  121. CGPoint stopPoint = CGPointMake(recognizer.view.center.x, recognizer.view.center.y);
  122. // 如果按钮超出屏幕边缘
  123. if (stopPoint.y + self.height + 40 >= SCREEN_HEIGHT) {
  124. stopPoint = CGPointMake(stopPoint.x, SCREEN_HEIGHT - self.height/2.0-TabBarHeight - 20);
  125. //这里注意iphoneX的适配。。X的SCREEN高度算法有变化。
  126. }
  127. if (stopPoint.x - self.width/2.0 <= 0) {
  128. stopPoint = CGPointMake(self.width/2.0, stopPoint.y);
  129. }
  130. if (stopPoint.x + self.width/2.0 >= SCREEN_WIDTH) {
  131. stopPoint = CGPointMake(SCREEN_WIDTH - self.width/2.0, stopPoint.y);
  132. }
  133. if (stopPoint.y - self.height/2.0 <= 40) {
  134. stopPoint = CGPointMake(stopPoint.x, self.height/2.0 + 50);
  135. }
  136. [UIView animateWithDuration:0.3 animations:^{
  137. recognizer.view.center = stopPoint;
  138. }];
  139. }
  140. break;
  141. default:
  142. break;
  143. }
  144. [recognizer setTranslation:CGPointMake(0, 0) inView:self.window];
  145. }
  146. - (UILabel *)timeLabel {
  147. if (!_timeLabel) {
  148. _timeLabel = [[UILabel alloc] init];
  149. _timeLabel.textColor = [UIColor colorWithHex:0x4AC7FA];
  150. _timeLabel.font = [UIFont systemFontOfSize:16];
  151. }return _timeLabel;
  152. }
  153. @end