MORightToLeftBannerView.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // MORightToLeftBannerView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/12/31.
  6. //
  7. static CGFloat const kMoBannerHeight = 25.0;
  8. static NSTimeInterval const kDisplayDuration = 2.5;
  9. static NSTimeInterval const kAnimationDuration = 0.4;
  10. static NSTimeInterval const kDismissAnimationDuration = 0.3;
  11. static NSTimeInterval const kDisplayDurationTwo = 3.0;
  12. static NSTimeInterval const kAnimationDurationTwo = 0.4;
  13. static NSTimeInterval const kSuperLuckyDisplayDuration = 5.0;
  14. static NSTimeInterval const kSuperLuckyAnimationDuration = 0.5;
  15. static NSTimeInterval const kSuperLuckyDismissAnimationDuration = 0.3;
  16. #import "MORightToLeftBannerView.h"
  17. #import "MOCrazyShowView.h"
  18. @interface MORightToLeftBannerView ()
  19. @property (nonatomic, strong) MOCrazyShowView *firstView;
  20. @property (nonatomic, strong) MOCrazyShowView *secondeView;
  21. /** 0 无动画 1 代表当前界面展示为firstView 2为当前界面展示为secondeView */
  22. @property (nonatomic, assign) NSInteger currentIndex;
  23. @end
  24. @implementation MORightToLeftBannerView
  25. - (instancetype)initWithFrame:(CGRect)frame{
  26. self = [super initWithFrame:frame];
  27. if (self){
  28. [self setupUI];
  29. }
  30. return self;
  31. }
  32. - (instancetype)initWithCoder:(NSCoder *)coder{
  33. self = [super initWithCoder:coder];
  34. if (self){
  35. [self setupUI];
  36. }
  37. return self;
  38. }
  39. - (void)setupUI{
  40. self.backgroundColor = [UIColor clearColor];
  41. [self addSubview:self.firstView];
  42. [self addSubview:self.secondeView];
  43. self.currentIndex = 0;
  44. }
  45. // 重写 hitTest:withEvent: 方法
  46. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  47. // 如果父视图的 userInteractionEnabled 为 NO 或者 alpha < 0.01 或者 hidden 为 YES,则直接返回 nil
  48. if (!self.userInteractionEnabled || self.alpha < 0.01 || self.hidden) {
  49. return nil;
  50. }
  51. // 检查触摸点是否在父视图内
  52. if (![self pointInside:point withEvent:event]) {
  53. return nil;
  54. }
  55. // 遍历子视图,检查是否有子视图可以响应触摸事件
  56. for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
  57. CGPoint convertedPoint = [subview convertPoint:point fromView:self];
  58. UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
  59. if (hitTestView) {
  60. return hitTestView;
  61. }
  62. }
  63. // 如果没有子视图响应触摸事件,父视图返回 nil 表示不响应事件
  64. return nil;
  65. }
  66. - (void)showMessage:(MORtmEntity *)rtmMessage {
  67. // Set message text
  68. [self toAddAndSortThe:rtmMessage];
  69. if(self.currentIndex == 0){
  70. MORtmEntity *theEntity = [[MOTopFloatingManager sharedManager] getNextTopFloatTwoRtmEntity];
  71. [self toBeginAnimationWith:self.firstView And:theEntity];
  72. self.currentIndex = 1;
  73. }
  74. }
  75. - (void)toAddAndSortThe:(MORtmEntity *)rtmMessage{
  76. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmMessage.data;
  77. MORtmUser *user = jsonEntity.luxury.user;
  78. //优先级 默认 客态5
  79. NSInteger stateBanner = GuestStateBannerThirdPriorityThird;
  80. if(jsonEntity.type == 4 ||
  81. jsonEntity.type == 17){//SuperLucky
  82. MORtmUser *luckyUser = jsonEntity.activitySuperLucky2.user;
  83. if([luckyUser.userId isEqualToString:GetUserId]){
  84. stateBanner = MainStateBannerThirdPriorityFirst;
  85. }
  86. else{
  87. stateBanner = GuestStateBannerThirdPriorityFirst;
  88. }
  89. }
  90. else if (jsonEntity.type == 10){//红包
  91. MORedEnvelopeObject *redObject = jsonEntity.redEnvelope;
  92. if([redObject.sendUserId isEqualToString:GetUserId]){
  93. stateBanner = MainStateBannerThirdPriorityFirst;
  94. }
  95. else{
  96. stateBanner = GuestStateBannerThirdPriorityFirst;
  97. }
  98. }
  99. else if (jsonEntity.type == 26){// 升级
  100. if([user.userId isEqualToString:GetUserId]){//自己
  101. stateBanner = MainStateBannerThirdPrioritySecond;
  102. }
  103. else{
  104. stateBanner = GuestStateBannerThirdPrioritySecond;
  105. }
  106. }
  107. else{//倍率飘屏
  108. MORtmUser *theUser = jsonEntity.giftSuperLucky.user;
  109. if([theUser.userId isEqualToString:GetUserId]){
  110. stateBanner = MainStateBannerThirdPriorityThird;
  111. }
  112. else{
  113. stateBanner = GuestStateBannerThirdPriorityThird;
  114. }
  115. }
  116. [[MOTopFloatingManager sharedManager] addEntity:rtmMessage toChannel:stateBanner];
  117. }
  118. - (void)toBeginAnimationWith:(MOCrazyShowView *)view And:(MORtmEntity *)rtmMessage{
  119. view.viewModel = rtmMessage;
  120. MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmMessage.data;
  121. //默认返钻中奖时间
  122. CGFloat beginAnimationDuration = kAnimationDuration;
  123. CGFloat displayDuration = kDisplayDuration;
  124. CGFloat dismissDuration = kDismissAnimationDuration;
  125. if(jsonEntity.type == 4 || jsonEntity.type == 17 || jsonEntity.type == 10){
  126. //SuperLucky / 红包
  127. beginAnimationDuration = kSuperLuckyAnimationDuration;
  128. displayDuration = kSuperLuckyDisplayDuration;
  129. dismissDuration = kSuperLuckyDismissAnimationDuration;
  130. }
  131. else if (jsonEntity.type == 26){// 升级
  132. beginAnimationDuration = kAnimationDurationTwo;
  133. displayDuration = kDisplayDurationTwo;
  134. }
  135. // Reset frame to initial position
  136. CGRect frame = view.frame;
  137. frame.origin.x = SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing;
  138. view.frame = frame;
  139. // Animate banner in
  140. [UIView animateWithDuration:beginAnimationDuration animations:^{
  141. CGRect frame = view.frame;
  142. frame.origin.x = kMOCrazyShowViewLeftAndRightSpacing;
  143. view.frame = frame;
  144. } completion:^(BOOL finished) {
  145. // After animation, wait for display duration
  146. [view toJudgeNeedAnimationScrollView];
  147. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(displayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  148. // Animate banner out
  149. [UIView animateWithDuration:dismissDuration animations:^{
  150. CGRect frame = view.frame;
  151. frame.origin.x = - SCREENWIDTH - kMOCrazyShowViewLeftAndRightSpacing;
  152. view.frame = frame;
  153. } completion:^(BOOL finished) {
  154. // Remove from superview after animation
  155. }];
  156. MORtmEntity *theEntity = [[MOTopFloatingManager sharedManager] getNextTopFloatTwoRtmEntity];
  157. if(theEntity){
  158. if(self.firstView == view){
  159. [self toBeginAnimationWith:self.secondeView And:theEntity];
  160. self.currentIndex = 2;
  161. }
  162. else{
  163. [self toBeginAnimationWith:self.firstView And:theEntity];
  164. self.currentIndex = 1;
  165. }
  166. }
  167. else{
  168. self.currentIndex = 0;
  169. }
  170. });
  171. }];
  172. }
  173. - (void)resetViewFrame{
  174. self.currentIndex = 0;
  175. self.firstView.frame = CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight);
  176. self.secondeView.frame = CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight);
  177. }
  178. #pragma mark - Lazy
  179. - (MOCrazyShowView *)firstView{
  180. if(!_firstView){
  181. WEAKSELF
  182. _firstView = [[MOCrazyShowView alloc] initWithFrame:CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight)];
  183. _firstView.throughRoomBlock = ^(MORtmEntity * _Nonnull viewModel) {
  184. weakSelf.throughRoomBlock ? weakSelf.throughRoomBlock(viewModel) : nil;
  185. };
  186. }
  187. return _firstView;
  188. }
  189. - (MOCrazyShowView *)secondeView{
  190. if(!_secondeView){
  191. WEAKSELF
  192. _secondeView = [[MOCrazyShowView alloc] initWithFrame:CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight)];
  193. _secondeView.throughRoomBlock = ^(MORtmEntity * _Nonnull viewModel) {
  194. weakSelf.throughRoomBlock ? weakSelf.throughRoomBlock(viewModel) : nil;
  195. };
  196. }
  197. return _secondeView;
  198. }
  199. @end