webRtcPlayerViewController+AdjustBtnFrame.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // webRtcPlayerViewController+AdjustBtnFrame.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/5.
  6. //
  7. #import "webRtcPlayerViewController+AdjustBtnFrame.h"
  8. #import <objc/runtime.h>
  9. @implementation webRtcPlayerViewController(AdjustBtnFrame)
  10. @dynamic adjustTime;
  11. - (void)setAdjustTime:(NSInteger)adjustTime {
  12. objc_setAssociatedObject(self, @selector(adjustTime), @(adjustTime), OBJC_ASSOCIATION_ASSIGN);
  13. }
  14. - (NSInteger)adjustTime {
  15. NSNumber *adjustTimeNumber = objc_getAssociatedObject(self, @selector(adjustTime));
  16. return [adjustTimeNumber integerValue];
  17. }
  18. #pragma mark 悬浮球3s后自动靠边隐藏
  19. // 控制按钮3s后 自动靠边算法
  20. - (void)adjustPlayerViewControlBtnFrame1 {
  21. CGFloat margin = 60/2.0;
  22. CGFloat W_H = 60;
  23. self.controlBtn.alpha = 0.5;
  24. CGPoint centerPoint = self.controlBtn.center;
  25. // HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(centerPoint));
  26. if (self.mediaStream.hw_w > self.mediaStream.hw_h){//横屏
  27. CGFloat screen_W = SCREEN_W>SCREEN_H?SCREEN_W:SCREEN_H;/*实际较大的数字*/
  28. CGFloat screen_H = (SCREEN_W<SCREEN_H?SCREEN_W:SCREEN_H);/*实际较小的数字*/
  29. CGFloat centerY = screen_W*0.5;
  30. CGFloat value1Y = centerY - centerPoint.x;
  31. //CGFloat adaptValue = (SCREEN_W - mPlayerView.glkshowImageView.hw_h)*0.5; // 补偿刘海屏上下黑边
  32. if (value1Y >= 0) { // 左边
  33. HLog(@"云手机推流界面 靠左边");
  34. self.controlBtn.hw_x = - W_H + margin + safeArea;
  35. }else { // 右边
  36. HLog(@"云手机推流界面 靠右边");
  37. self.controlBtn.hw_x = screen_W - margin;
  38. }
  39. if (centerPoint.y < 0){
  40. self.controlBtn.hw_y = 0;
  41. }else if (self.controlBtn.hw_y > screen_H){
  42. self.controlBtn.hw_y = screen_H - W_H;
  43. }
  44. }else{//竖屏
  45. //竖屏靠边状态 不处理
  46. if(centerPoint.x == 0 || centerPoint.x == SCREEN_W){
  47. return;
  48. }
  49. CGFloat centerX = SCREEN_W*0.5;
  50. CGFloat value1X = centerX - centerPoint.x;
  51. if (value1X >= 0) { // 左边
  52. HLog(@"云手机推流界面 靠左边");
  53. self.controlBtn.hw_x = - W_H + margin;
  54. }else { // 右边
  55. HLog(@"云手机推流界面 靠右边");
  56. self.controlBtn.hw_x = SCREEN_W - margin;
  57. }
  58. //原来Y坐标逻辑
  59. // if (self.controlBtn.y < margin){
  60. // self.controlBtn.y = margin;
  61. // }else if (self.controlBtn.y > self.mediaStream.height - margin - W_H){
  62. // self.controlBtn.y = self.mediaStream.height - margin - W_H;
  63. // }
  64. //20240905 Y坐标逻辑
  65. // if(centerPoint.y < (self.mediaStream.hw_y + margin)){//在屏幕上方 超出了推流页
  66. // self.controlBtn.hw_y = self.mediaStream.hw_y + margin;
  67. // }
  68. // else if(centerPoint.y > (self.mediaStream.hw_max_y)
  69. // && (SCREEN_H - centerPoint.y) < safeArea){//在屏幕下方 超出了推流页
  70. // self.controlBtn.hw_y = self.mediaStream.hw_h - margin;
  71. // }
  72. // else{//在屏幕中间
  73. // //Y 不变
  74. // }
  75. }
  76. didAdjusBtnType = YES;
  77. HLog(@"云手机推流界面 控制按钮中点%@",NSStringFromCGPoint(self.controlBtn.center));
  78. }
  79. // 1、悬浮球3s后 自动靠边隐藏
  80. - (void)extensionAdjustBtnFrameCheckAdjustTime {
  81. self.adjustTime--;
  82. if(self.adjustTime < 0){
  83. self.adjustTime = 0;
  84. }
  85. //if (self.adjustTime == 0 && !didAdjusBtnType)
  86. if (self.adjustTime == 0)
  87. {
  88. // HLog(@"云手机圆形浮标自动隐藏倒计时 3s倒计时结束");
  89. [self adjustPlayerViewControlBtnFrame1];
  90. }else {
  91. // HLog(@"云手机圆形浮标自动隐藏倒计时:%zd", self.adjustTime);
  92. }
  93. }
  94. #pragma mark 悬浮标拖拽
  95. - (void)controlBtnPanGestureCallback:(UIPanGestureRecognizer *)panGesture{
  96. self.controlBtn.alpha = 1;
  97. CGRect frame = self.controlBtn.frame;
  98. switch (panGesture.state) {
  99. case UIGestureRecognizerStateBegan:{
  100. startFrame = self.controlBtn.frame;
  101. break;
  102. }
  103. case UIGestureRecognizerStateChanged:{
  104. self.adjustTime = 2;
  105. CGPoint translatedPoint = [panGesture translationInView:self.mediaStream];/*KXLY*/
  106. CGRect frame = self.controlBtn.frame;
  107. frame.origin.x = translatedPoint.x + startFrame.origin.x;
  108. frame.origin.y = translatedPoint.y + startFrame.origin.y;
  109. self.controlBtn.frame = frame;
  110. // HLog(@"\n--(%f,%f)--controlBtn.frame--X-%f",translatedPoint.x,translatedPoint.x,controlBtn.frame.origin.x);
  111. break;
  112. }
  113. case UIGestureRecognizerStateEnded:{
  114. self.adjustTime = 1;
  115. // HLog(@"云手机推流界面 控制按钮拖动手势结束");
  116. /*防止控制器移出屏幕*/
  117. if (frame.origin.x < 10.f/2.f){
  118. frame.origin.x = 10.f/2.f;
  119. }
  120. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  121. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  122. }
  123. if (frame.size.width + frame.origin.x + 10.f/2.f > self.view.hw_w){
  124. frame.origin.x = self.view.hw_w - 10.f/2.f - frame.size.width;
  125. }
  126. if (frame.size.height + frame.origin.y + 10.f/2.f > self.view.hw_h){
  127. frame.origin.y = self.view.hw_h - 10.f/2.f - frame.size.height;
  128. }
  129. self.controlBtn.frame = frame;
  130. }
  131. break;
  132. case UIGestureRecognizerStateCancelled: {
  133. /*防止控制器移出屏幕*/
  134. if (frame.origin.x < 10.f/2.f){
  135. frame.origin.x = 10.f/2.f;
  136. }
  137. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  138. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  139. }
  140. if (frame.size.width + frame.origin.x + 10.f/2.f > self.view.hw_w){
  141. frame.origin.x = self.view.hw_w - 10.f/2.f - frame.size.width;
  142. }
  143. if (frame.size.height + frame.origin.y + 10.f/2.f > self.view.hw_h){
  144. frame.origin.y = self.view.hw_h - 10.f/2.f - frame.size.height;
  145. }
  146. self.controlBtn.frame = frame;
  147. break;
  148. }
  149. default:
  150. break;
  151. }
  152. }
  153. @end