webRtcPlayerViewController+AdjustBtnFrame.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // HLog(@"云手机圆形浮标自动隐藏倒计时 3s倒计时结束");
  87. [self adjustPlayerViewControlBtnFrame1];
  88. }else {
  89. // HLog(@"云手机圆形浮标自动隐藏倒计时:%zd", self.adjustTime);
  90. }
  91. }
  92. #pragma mark 悬浮标拖拽
  93. - (void)controlBtnPanGestureCallback:(UIPanGestureRecognizer *)panGesture{
  94. self.controlBtn.alpha = 1;
  95. CGRect frame = self.controlBtn.frame;
  96. switch (panGesture.state) {
  97. case UIGestureRecognizerStateBegan:{
  98. startFrame = self.controlBtn.frame;
  99. break;
  100. }
  101. case UIGestureRecognizerStateChanged:{
  102. self.adjustTime = 2;
  103. CGPoint translatedPoint = [panGesture translationInView:self.mediaStream];/*KXLY*/
  104. CGRect frame = self.controlBtn.frame;
  105. frame.origin.x = translatedPoint.x + startFrame.origin.x;
  106. frame.origin.y = translatedPoint.y + startFrame.origin.y;
  107. self.controlBtn.frame = frame;
  108. // HLog(@"\n--(%f,%f)--controlBtn.frame--X-%f",translatedPoint.x,translatedPoint.x,controlBtn.frame.origin.x);
  109. break;
  110. }
  111. case UIGestureRecognizerStateEnded:{
  112. self.adjustTime = 1;
  113. // HLog(@"云手机推流界面 控制按钮拖动手势结束");
  114. /*防止控制器移出屏幕*/
  115. if (frame.origin.x < 10.f/2.f){
  116. frame.origin.x = 10.f/2.f;
  117. }
  118. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  119. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  120. }
  121. if (frame.size.width + frame.origin.x + 10.f/2.f > self.view.hw_w){
  122. frame.origin.x = self.view.hw_w - 10.f/2.f - frame.size.width;
  123. }
  124. if (frame.size.height + frame.origin.y + 10.f/2.f > self.view.hw_h){
  125. frame.origin.y = self.view.hw_h - 10.f/2.f - frame.size.height;
  126. }
  127. self.controlBtn.frame = frame;
  128. }
  129. break;
  130. case UIGestureRecognizerStateCancelled: {
  131. /*防止控制器移出屏幕*/
  132. if (frame.origin.x < 10.f/2.f){
  133. frame.origin.x = 10.f/2.f;
  134. }
  135. if (frame.origin.y < 10.f/2.f + H_STATE_BAR){
  136. frame.origin.y = 10.f/2.f + H_STATE_BAR;
  137. }
  138. if (frame.size.width + frame.origin.x + 10.f/2.f > self.view.hw_w){
  139. frame.origin.x = self.view.hw_w - 10.f/2.f - frame.size.width;
  140. }
  141. if (frame.size.height + frame.origin.y + 10.f/2.f > self.view.hw_h){
  142. frame.origin.y = self.view.hw_h - 10.f/2.f - frame.size.height;
  143. }
  144. self.controlBtn.frame = frame;
  145. break;
  146. }
  147. default:
  148. break;
  149. }
  150. }
  151. @end