MOLiveSmallView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // MOLiveSmallView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/4/15.
  6. //
  7. #import "MOLiveSmallView.h"
  8. #import "MOForbidScreenShotView.h"
  9. static MOLiveSmallView *_sharedSingleton = nil;
  10. static dispatch_once_t onceToken;
  11. @interface MOLiveSmallView ()
  12. @property (nonatomic, strong) UIView *localView;
  13. @property (nonatomic, strong) BigBtn *closeBtn;
  14. @end
  15. @implementation MOLiveSmallView
  16. + (instancetype)sharedSingleton
  17. {
  18. static dispatch_once_t onceTokenK;
  19. dispatch_once(&onceTokenK, ^
  20. {
  21. // 要使用self来调用
  22. _sharedSingleton = [[self alloc] init];
  23. });
  24. return _sharedSingleton;
  25. }
  26. - (instancetype)init{
  27. self = [super init];
  28. if (self)
  29. {
  30. [self setupUI];
  31. }
  32. return self;
  33. }
  34. - (void)setupUI{
  35. UIView *superView = self;
  36. if (@available(iOS 13.2, *)) {
  37. UIView *tempView = [MOForbidScreenShotView creactWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  38. tempView.backgroundColor = [UIColor clearColor];
  39. [self addSubview:tempView];
  40. [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.edges.equalTo(self);
  42. }];
  43. superView = tempView;
  44. }
  45. [superView addSubview:self.localView];
  46. [self.localView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.edges.equalTo(self);
  48. }];
  49. [self addSubview:self.closeBtn];
  50. [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.top.equalTo(self).offset(10.0);
  52. make.right.equalTo(self).offset(-10.0);
  53. make.width.height.equalTo(@15.0);
  54. }];
  55. }
  56. - (void)setLiveModel:(MOLiveDetail *)liveModel{
  57. _liveModel = liveModel;
  58. WEAKSELF
  59. NSInteger agoraId = GetAgoraId;
  60. if(self.isCreatLive){
  61. [[MOShowAgoraKitManager shareManager] setupLocalVideoWithUid:agoraId AndCanvasView:self.localView];
  62. }
  63. else{
  64. [[MOShowAgoraKitManager shareManager] setupRemoteVideoWidhChannelId:liveModel.currentRoom.id UId:liveModel.currentRoom.anchorUser.agoraId CanvasView:self.localView];
  65. }
  66. }
  67. - (void)toShowTheLivingRoomWith:(MOShowLiveVC *)vc{
  68. self.showVC = vc;
  69. self.isSmallShow = YES;
  70. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  71. [keyWindow addSubview:self];
  72. CGFloat normalWidth = SCREENWIDTH;
  73. CGFloat normalHeight = SCREENHEIGHT;
  74. CGFloat newWidth = normalWidth / 4.0;
  75. CGFloat newHeight = normalHeight / 4.0;
  76. CGFloat x = normalWidth - newWidth - 10.0;
  77. CGFloat y = normalHeight - newHeight - 80.0;
  78. self.frame = CGRectMake(x, y, newWidth, newHeight);
  79. }
  80. #pragma mark ----- touchDelegate
  81. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  82. {
  83. self.center = [touches.anyObject locationInView:self.superview];
  84. }
  85. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  86. {
  87. [self endTouch:[touches.anyObject locationInView:self.superview]];
  88. }
  89. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  90. {
  91. [self endTouch:[touches.anyObject locationInView:self.superview]];
  92. }
  93. //吸附功能
  94. - (void)endTouch:(CGPoint)point
  95. {
  96. CGRect frame = self.frame;
  97. CGFloat width;
  98. CGFloat height;
  99. width = self.superview.height;
  100. height = self.superview.width;
  101. if (point.x > width / 2)
  102. {
  103. frame.origin.x = width - frame.size.width - 10.0;
  104. }
  105. else
  106. {
  107. frame.origin.x = 10.0;
  108. }
  109. if (frame.origin.y > height - 64 - frame.size.height)
  110. {
  111. frame.origin.y = height - 64 - frame.size.height;
  112. }
  113. else if (frame.origin.y < (self.superview.y + 10.0))
  114. {
  115. frame.origin.y = self.superview.y + 10.0;
  116. }
  117. [UIView animateWithDuration:0.3 animations:^
  118. {
  119. self.frame = frame;
  120. }];
  121. }
  122. #pragma mark - Lazy
  123. - (UIView *)localView{
  124. if(!_localView){
  125. _localView = [[UIView alloc] init];
  126. }
  127. return _localView;
  128. }
  129. - (BigBtn *)closeBtn{
  130. if (!_closeBtn)
  131. {
  132. _closeBtn = [BigBtn buttonWithType:UIButtonTypeCustom];
  133. _closeBtn.backgroundColor = [UIColor clearColor];
  134. [_closeBtn setImage:[UIImage imageNamed:@"icon_live_close"] forState:UIControlStateNormal];
  135. [_closeBtn addTarget:self action:@selector(closeWishBtnClick) forControlEvents:UIControlEventTouchUpInside];
  136. }
  137. return _closeBtn;
  138. }
  139. - (void)closeWishBtnClick{
  140. self.isSmallShow = NO;
  141. //判断视图中是否有直播间VC, 如果在外部则直接执行退出直播间操作, 如果有则隐藏小直播窗口
  142. }
  143. @end