// // AnchorRoomScrollView.m // xplan-ios // // Created by GreenLand on 2022/7/11. // #import "AnchorRoomScrollView.h" #import "XPMacro.h" #import "RoomInfoModel.h" //SVGA动画播放 #import "SVGAParserManager.h" #import @interface AnchorRoomScrollView() ///动态背景 @property (nonatomic, strong) SVGAImageView *middleDisplayView; @property (nonatomic, strong) SVGAParserManager *parserManager; @property (nonatomic, strong) SVGAImageView *topDisplayView; @property (nonatomic, strong) SVGAImageView *downDisplayView; @property (nonatomic, assign) BOOL animatePlay; @end @implementation AnchorRoomScrollView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if(self) { self.backgroundColor = [UIColor clearColor]; self.pagingEnabled = YES; self.showsHorizontalScrollIndicator = NO; self.showsVerticalScrollIndicator = NO; self.delegate = self; self.alwaysBounceVertical = NO; self.bounces = NO; self.delaysContentTouches = NO; self.scrollsToTop = NO; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(roomMessageTabelViewStopScroll) name:@"roomMessageTabelViewStopScroll" object:nil]; self.isCanScrollTop = NO; [self addSubview:self.topDisplayView]; [self addSubview:self.middleDisplayView]; [self addSubview:self.downDisplayView]; } return self; } - (void)startAnimate:(SVGAImageView *)displayView { self.animatePlay = YES; NSString *bgString = [[NSBundle mainBundle] pathForResource:@"anchor_room_bg" ofType:@"svga"]; NSURL *bgUrl = [NSURL fileURLWithPath:bgString]; [self.parserManager loadSvgaWithURL:bgUrl completionBlock:^(SVGAVideoEntity * _Nullable videoItem) { if (videoItem != nil) { CGFloat width = videoItem.videoSize.width; CGFloat height = videoItem.videoSize.height; if (width > height) { displayView.contentMode = UIViewContentModeScaleAspectFit; } else {//高大于宽 CGFloat resizeH = KScreenWidth * height / width;//按照屏幕的宽度去缩放,获得高度 if (resizeH > KScreenHeight) {//如果大于屏幕高度,填充 displayView.contentMode = UIViewContentModeScaleAspectFill; } else {//小于屏幕高度, displayView.contentMode = UIViewContentModeScaleAspectFit; } } displayView.loops = INT_MAX; displayView.clearsAfterStop = NO; displayView.videoItem = videoItem; [displayView startAnimation]; } } failureBlock:^(NSError * _Nullable error) { }]; } - (void)stopAnimate { [self.middleDisplayView stopAnimation]; [self.topDisplayView stopAnimation]; [self.downDisplayView stopAnimation]; self.animatePlay = NO; } - (void)roomMessageTabelViewStopScroll { self.scrollEnabled = YES; } - (void)switchPlayer:(UIScrollView*)scrollView { CGFloat offset = scrollView.contentOffset.y; if (offset >= self.frame.size.height * 2) { if (self.anchorScrollDelegate && [self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollViewScrollToNext:)]) { [self.anchorScrollDelegate anchorScrollViewScrollToNext:self]; } [self stopAnimate]; } else if (offset <= 0) { if (self.anchorScrollDelegate && [self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollViewScrollToPrevious:)]) { [self.anchorScrollDelegate anchorScrollViewScrollToPrevious:self]; } [self stopAnimate]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y > KScreenHeight) {///乡上滚动 if (!self.animatePlay) { [self startAnimate:self.downDisplayView]; } } else {///向下滚动 if (self.isCanScrollTop) { if (!self.animatePlay) { [self startAnimate:self.topDisplayView]; } } else { ///刚进来的时候 还不能向上滚动的时候 if (!self.animatePlay) { [self startAnimate:self.downDisplayView]; } } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (self.isCanScrollTop) { if (scrollView.contentOffset.y >= KScreenHeight * 2) { if (self.anchorScrollDelegate && [self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollViewScrollToNext:)]) { [self.anchorScrollDelegate anchorScrollViewScrollToNext:self]; } } else if(scrollView.contentOffset.y <= 0) { if (self.anchorScrollDelegate && [self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollViewScrollToPrevious:)]) { [self.anchorScrollDelegate anchorScrollViewScrollToPrevious:self]; } } } else { if (scrollView.contentOffset.y >= KScreenHeight) { if (self.anchorScrollDelegate && [self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollViewScrollToNext:)]) { [self.anchorScrollDelegate anchorScrollViewScrollToNext:self]; } } } [self stopAnimate]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { ///666是外部的公屏消息列表 8888房间半屏弹框顶部消失的view if (otherGestureRecognizer.view.tag == 666 || otherGestureRecognizer.view.tag == 8888) { self.scrollEnabled = NO; return NO; } self.scrollEnabled = YES; return YES; } - (void)setIsCanScrollTop:(BOOL)isCanScrollTop { _isCanScrollTop = isCanScrollTop; CGRect frame = self.frame; if (_isCanScrollTop) { self.contentSize = CGSizeMake(0, frame.size.height * 3); self.topDisplayView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); self.middleDisplayView.frame = CGRectMake(0, frame.size.height, frame.size.width, frame.size.height); self.downDisplayView.frame = CGRectMake(0, frame.size.height * 2, frame.size.width, frame.size.height); self.middleImageView = self.middleDisplayView; } else { self.contentSize = CGSizeMake(0, frame.size.height * 2); self.topDisplayView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); self.middleDisplayView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); self.downDisplayView.frame = CGRectMake(0, frame.size.height, frame.size.width, frame.size.height); self.middleImageView = self.middleDisplayView; } } - (SVGAImageView *)middleDisplayView { if (_middleDisplayView == nil) { _middleDisplayView = [[SVGAImageView alloc] init]; _middleDisplayView.contentMode = UIViewContentModeScaleToFill; _middleDisplayView.userInteractionEnabled = NO; _middleDisplayView.backgroundColor = [UIColor clearColor]; } return _middleDisplayView; } - (SVGAImageView *)topDisplayView { if (_topDisplayView == nil) { _topDisplayView = [[SVGAImageView alloc] init]; _topDisplayView.contentMode = UIViewContentModeScaleToFill; _topDisplayView.userInteractionEnabled = NO; _topDisplayView.backgroundColor = [UIColor clearColor]; } return _topDisplayView; } - (SVGAImageView *)downDisplayView { if (_downDisplayView == nil) { _downDisplayView = [[SVGAImageView alloc] init]; _downDisplayView.contentMode = UIViewContentModeScaleToFill; _downDisplayView.userInteractionEnabled = NO; _downDisplayView.backgroundColor = [UIColor clearColor]; } return _downDisplayView; } - (SVGAParserManager *)parserManager { if (!_parserManager) { _parserManager = [[SVGAParserManager alloc] init]; } return _parserManager; } @end