个播房上下滑动

This commit is contained in:
chenguilong
2022-07-11 19:05:09 +08:00
parent 521884c1b7
commit f6a3b8c6dd
8 changed files with 510 additions and 4 deletions

View File

@@ -0,0 +1,204 @@
//
// AnchorRoomScrollView.m
// xplan-ios
//
// Created by GreenLand on 2022/7/11.
//
#import "AnchorRoomScrollView.h"
@interface AnchorRoomScrollView()<UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSMutableArray * lives;
//@property (nonatomic, strong) NSString *live;
@property (nonatomic, strong) UIImageView *downImageView;
//@property (nonatomic, strong) NSString *upperLive, *middleLive, *downLive;
@property (nonatomic, strong) NSString *upperLive, *middleLive, *downLive;
@property (nonatomic, assign) NSInteger currentIndex;
@end
@implementation AnchorRoomScrollView
- (NSMutableArray *)lives
{
if (!_lives) {
_lives = [NSMutableArray array];
}
return _lives;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.contentSize = CGSizeMake(0, frame.size.height * 3);
self.contentOffset = CGPointMake(0, frame.size.height);
self.pagingEnabled = YES;
self.opaque = YES;
self.backgroundColor = [UIColor yellowColor];
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.delegate = self;
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
self.tag = 1000;
// self.delaysContentTouches = NO;
// self.canCancelContentTouches = NO;
// image views
// blur effect
// UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// blur view
// UIVisualEffectView *visualEffectViewUpper = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
// UIVisualEffectView *visualEffectViewMiddle = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
// UIVisualEffectView *visualEffectViewDown = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
self.upperImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, frame.size.height)];
self.downImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height*2, frame.size.width, frame.size.height)];
// add image views
[self addSubview:self.upperImageView];
[self addSubview:self.middleImageView];
[self addSubview:self.downImageView];
// visualEffectViewUpper.frame = self.upperImageView.frame;
// [self addSubview:visualEffectViewUpper];
// visualEffectViewMiddle.frame = self.middleImageView.frame;
// [self addSubview:visualEffectViewMiddle];
// visualEffectViewDown.frame = self.downImageView.frame;
// [self addSubview:visualEffectViewDown];
}
return self;
}
- (void)updateForLives:(NSMutableArray *)livesArray withCurrentIndex:(NSInteger)index
{
if (livesArray.count && [livesArray firstObject]) {
[self.lives removeAllObjects];
[self.lives addObjectsFromArray:livesArray];
self.currentIndex = index;
_upperLive = [[NSString alloc] init];
_middleLive = (NSString *)_lives[_currentIndex];
_downLive = [[NSString alloc] init];
if (_currentIndex == 0) {
_upperLive = (NSString *)[_lives lastObject];
} else {
_upperLive = (NSString *)_lives[_currentIndex - 1];
}
if (_currentIndex == _lives.count - 1) {
_downLive = (NSString *)[_lives firstObject];
} else {
_downLive = (NSString *)_lives[_currentIndex + 1];
}
[self prepareForImageView:self.upperImageView withLive:_upperLive];
[self prepareForImageView:self.middleImageView withLive:_middleLive];
[self prepareForImageView:self.downImageView withLive:_downLive];
}
self.scrollEnabled = livesArray.count > 1;
}
- (void) prepareForImageView: (UIImageView *)imageView withLive:(NSString *)live
{
// download images for ImageView.
imageView.image = [UIImage imageNamed:@"room_background"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
}
- (void)switchPlayer:(UIScrollView*)scrollView
{
CGFloat offset = scrollView.contentOffset.y;
if (self.lives.count) {
if (offset >= 2*self.frame.size.height)
{
// slides to the down player
scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
_currentIndex++;
self.upperImageView.image = self.middleImageView.image;
self.middleImageView.image = self.downImageView.image;
if (_currentIndex == self.lives.count - 1)
{
_downLive = [self.lives firstObject];
} else if (_currentIndex == self.lives.count)
{
_downLive = self.lives[1];
_currentIndex = 0;
} else
{
_downLive = self.lives[_currentIndex+1];
}
[self prepareForImageView:self.downImageView withLive:_downLive];
if ([self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollView:currentPlayerIndex:)]) {
[self.anchorScrollDelegate anchorScrollView:self currentPlayerIndex:_currentIndex];
}
}
else if (offset <= 0)
{
// slides to the upper player
scrollView.contentOffset = CGPointMake(0, self.frame.size.height);
_currentIndex--;
self.downImageView.image = self.middleImageView.image;
self.middleImageView.image = self.upperImageView.image;
if (_currentIndex == 0)
{
_upperLive = [self.lives lastObject];
} else if (_currentIndex == -1)
{
_upperLive = self.lives[self.lives.count - 2];
_currentIndex = self.lives.count-1;
} else
{
_upperLive = self.lives[_currentIndex - 1];
}
[self prepareForImageView:self.upperImageView withLive:_upperLive];
if ([self.anchorScrollDelegate respondsToSelector:@selector(anchorScrollView:currentPlayerIndex:)]) {
[self.anchorScrollDelegate anchorScrollView:self currentPlayerIndex:_currentIndex];
}
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%@", [scrollView class]);
[self switchPlayer:scrollView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
}
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// UIView *view = [super hitTest:point withEvent:event];
// return view == self?nil:view;
//}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
///1000scrollView 1001collectionView
if (otherGestureRecognizer.view.tag == 999) {
// self.scrollEnabled = NO;
// if (self.contentOffset.y != 0) {
// self.contentOffset = CGPointMake(0, 0);;
// }
return NO;
} else {
// self.scrollEnabled = YES;
}
return YES;
if (gestureRecognizer.state == 0) {
return YES;
} else {
return NO;
}
}
// called when a gesture recognizer attempts to transition out of UIGestureRecognizerStatePossible. returning NO causes it to transition to UIGestureRecognizerStateFailed
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// return YES;
//}
@end