// // MORightToLeftBannerView.m // MiMoLive // // Created by SuperC on 2023/12/31. // static CGFloat const kMoBannerHeight = 25.0; static NSTimeInterval const kDisplayDuration = 2.5; static NSTimeInterval const kAnimationDuration = 0.4; static NSTimeInterval const kDismissAnimationDuration = 0.3; static NSTimeInterval const kDisplayDurationTwo = 3.0; static NSTimeInterval const kAnimationDurationTwo = 0.4; static NSTimeInterval const kSuperLuckyDisplayDuration = 5.0; static NSTimeInterval const kSuperLuckyAnimationDuration = 0.5; static NSTimeInterval const kSuperLuckyDismissAnimationDuration = 0.3; #import "MORightToLeftBannerView.h" #import "MOCrazyShowView.h" @interface MORightToLeftBannerView () @property (nonatomic, strong) MOCrazyShowView *firstView; @property (nonatomic, strong) MOCrazyShowView *secondeView; /** 0 无动画 1 代表当前界面展示为firstView 2为当前界面展示为secondeView */ @property (nonatomic, assign) NSInteger currentIndex; @end @implementation MORightToLeftBannerView - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self){ [self setupUI]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder{ self = [super initWithCoder:coder]; if (self){ [self setupUI]; } return self; } - (void)setupUI{ self.backgroundColor = [UIColor clearColor]; [self addSubview:self.firstView]; [self addSubview:self.secondeView]; self.currentIndex = 0; } // 重写 hitTest:withEvent: 方法 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { // 如果父视图的 userInteractionEnabled 为 NO 或者 alpha < 0.01 或者 hidden 为 YES,则直接返回 nil if (!self.userInteractionEnabled || self.alpha < 0.01 || self.hidden) { return nil; } // 检查触摸点是否在父视图内 if (![self pointInside:point withEvent:event]) { return nil; } // 遍历子视图,检查是否有子视图可以响应触摸事件 for (UIView *subview in [self.subviews reverseObjectEnumerator]) { CGPoint convertedPoint = [subview convertPoint:point fromView:self]; UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event]; if (hitTestView) { return hitTestView; } } // 如果没有子视图响应触摸事件,父视图返回 nil 表示不响应事件 return nil; } - (void)showMessage:(MORtmEntity *)rtmMessage { // Set message text [self toAddAndSortThe:rtmMessage]; if(self.currentIndex == 0){ MORtmEntity *theEntity = [[MOTopFloatingManager sharedManager] getNextTopFloatTwoRtmEntity]; [self toBeginAnimationWith:self.firstView And:theEntity]; self.currentIndex = 1; } } - (void)toAddAndSortThe:(MORtmEntity *)rtmMessage{ MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmMessage.data; MORtmUser *user = jsonEntity.luxury.user; //优先级 默认 客态5 NSInteger stateBanner = GuestStateBannerThirdPriorityThird; if(jsonEntity.type == 4 || jsonEntity.type == 17){//SuperLucky MORtmUser *luckyUser = jsonEntity.activitySuperLucky2.user; if([luckyUser.userId isEqualToString:GetUserId]){ stateBanner = MainStateBannerThirdPriorityFirst; } else{ stateBanner = GuestStateBannerThirdPriorityFirst; } } else if (jsonEntity.type == 10){//红包 MORedEnvelopeObject *redObject = jsonEntity.redEnvelope; if([redObject.sendUserId isEqualToString:GetUserId]){ stateBanner = MainStateBannerThirdPriorityFirst; } else{ stateBanner = GuestStateBannerThirdPriorityFirst; } } else if (jsonEntity.type == 26){// 升级 if([user.userId isEqualToString:GetUserId]){//自己 stateBanner = MainStateBannerThirdPrioritySecond; } else{ stateBanner = GuestStateBannerThirdPrioritySecond; } } else{//倍率飘屏 MORtmUser *theUser = jsonEntity.giftSuperLucky.user; if([theUser.userId isEqualToString:GetUserId]){ stateBanner = MainStateBannerThirdPriorityThird; } else{ stateBanner = GuestStateBannerThirdPriorityThird; } } [[MOTopFloatingManager sharedManager] addEntity:rtmMessage toChannel:stateBanner]; } - (void)toBeginAnimationWith:(MOCrazyShowView *)view And:(MORtmEntity *)rtmMessage{ view.viewModel = rtmMessage; MORtmJosnEntity *jsonEntity = (MORtmJosnEntity *)rtmMessage.data; //默认返钻中奖时间 CGFloat beginAnimationDuration = kAnimationDuration; CGFloat displayDuration = kDisplayDuration; CGFloat dismissDuration = kDismissAnimationDuration; if(jsonEntity.type == 4 || jsonEntity.type == 17 || jsonEntity.type == 10){ //SuperLucky / 红包 beginAnimationDuration = kSuperLuckyAnimationDuration; displayDuration = kSuperLuckyDisplayDuration; dismissDuration = kSuperLuckyDismissAnimationDuration; } else if (jsonEntity.type == 26){// 升级 beginAnimationDuration = kAnimationDurationTwo; displayDuration = kDisplayDurationTwo; } // Reset frame to initial position CGRect frame = view.frame; frame.origin.x = SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing; view.frame = frame; // Animate banner in [UIView animateWithDuration:beginAnimationDuration animations:^{ CGRect frame = view.frame; frame.origin.x = kMOCrazyShowViewLeftAndRightSpacing; view.frame = frame; } completion:^(BOOL finished) { // After animation, wait for display duration [view toJudgeNeedAnimationScrollView]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(displayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // Animate banner out [UIView animateWithDuration:dismissDuration animations:^{ CGRect frame = view.frame; frame.origin.x = - SCREENWIDTH - kMOCrazyShowViewLeftAndRightSpacing; view.frame = frame; } completion:^(BOOL finished) { // Remove from superview after animation }]; MORtmEntity *theEntity = [[MOTopFloatingManager sharedManager] getNextTopFloatTwoRtmEntity]; if(theEntity){ if(self.firstView == view){ [self toBeginAnimationWith:self.secondeView And:theEntity]; self.currentIndex = 2; } else{ [self toBeginAnimationWith:self.firstView And:theEntity]; self.currentIndex = 1; } } else{ self.currentIndex = 0; } }); }]; } - (void)resetViewFrame{ self.currentIndex = 0; self.firstView.frame = CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight); self.secondeView.frame = CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight); } #pragma mark - Lazy - (MOCrazyShowView *)firstView{ if(!_firstView){ WEAKSELF _firstView = [[MOCrazyShowView alloc] initWithFrame:CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight)]; _firstView.throughRoomBlock = ^(MORtmEntity * _Nonnull viewModel) { weakSelf.throughRoomBlock ? weakSelf.throughRoomBlock(viewModel) : nil; }; } return _firstView; } - (MOCrazyShowView *)secondeView{ if(!_secondeView){ WEAKSELF _secondeView = [[MOCrazyShowView alloc] initWithFrame:CGRectMake(SCREENWIDTH + kMOCrazyShowViewLeftAndRightSpacing, 0, SCREENWIDTH - 2 * kMOCrazyShowViewLeftAndRightSpacing, kMoBannerHeight)]; _secondeView.throughRoomBlock = ^(MORtmEntity * _Nonnull viewModel) { weakSelf.throughRoomBlock ? weakSelf.throughRoomBlock(viewModel) : nil; }; } return _secondeView; } @end