TableView上下滑动 (仿->短视频,直播 )
2021-09-06 本文已影响0人
失忆的程序员
直接上代码,copy完记得点个赞。
.h
// 一张一张 滑动
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface XTableViewVC : UIViewController
@end
NS_ASSUME_NONNULL_END
.m
// 一张一张 滑动
#import "XTableViewVC.h"
#define X_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define X_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define X_STATUS_BAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height
static NSUInteger kDataSourceCount = 20;
@interface XTableViewVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger currentIndex;
@end
@implementation XTableViewVC
#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO;
}
#pragma mark -
- (void)dealloc {
[self.tableView.layer removeAllAnimations];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self removeObserver:self forKeyPath:@"currentIndex"];
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 释放 播放view
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setBackgroundImage:@"图片1"]; // 占位
[self setUpView];
}
#pragma mark -
- (void)setBackgroundImage:(NSString *)imageName {
UIImageView *background = [[UIImageView alloc] initWithFrame:self.view.bounds];
background.clipsToBounds = YES;
background.contentMode = UIViewContentModeScaleAspectFill;
background.image = [UIImage imageNamed:imageName];
[self.view addSubview:background];
}
#pragma mark -
- (instancetype)init {
self = [super init];
if (self) {
self.currentIndex = 0;
// 电池条 点击
/*
方法二 : 不可以点击
// 取消 tableview 点击状态栏 自动回弹到顶部
_tableView.scrollsToTop = NO;
*/
/*
方法一 : 可以点击
AppDelegate 中 加入这个
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
//当触摸状态栏的时候发送触摸通知 这样控制器就收到了点击事件
CGPoint touchLocation = [[[event allTouches] anyObject] locationInView:self.window];
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if (CGRectContainsPoint(statusBarFrame, touchLocation)) {
[[NSNotificationCenter defaultCenter] postNotificationName:StatusBarTouchBeginNotification object:nil];
}
}
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarTouchBegin) name:StatusBarTouchBeginNotification object:nil];
/*
(4)回调方法:applicationDidEnterBackground:
本地通知:UIApplicationDidEnterBackgroundNotification
触发时机:程序进入后台时调用。
适宜操作:这个阶段应该保存用户数据,释放一些资源(例如释放数据库资源)。
(5)回调方法:applicationWillEnterForeground:
本地通知:UIApplicationWillEnterForegroundNotification
触发时机:程序进入前台,但是还没有处于活动状态时调用。
适宜操作:这个阶段应该恢复用户数据。
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecomeActive) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name: UIApplicationDidEnterBackgroundNotification object:nil];
}
return self;
}
#pragma mark -
#pragma mark - event response 所有触发的事件响应 按钮、通知、分段控件等
- (void)statusBarTouchBegin {
self.currentIndex = 0; //KVO
}
- (void)applicationBecomeActive {
// UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0]];
// if(!_isCurPlayerPause) {
// [cell.playerView play];
// }
}
- (void)applicationEnterBackground {
// UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0]];
// _isCurPlayerPause = ![cell.playerView rate];
// [cell.playerView pause];
}
//观察currentIndex变化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"currentIndex"]) {
//获取当前显示的cell
// UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0]];
// __weak typeof (cell) wcell = cell;
// __weak typeof (self) wself = self;
//用cell控制相关视频播放
} else {
return [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark -
#pragma mark - private methods 私有方法
- (void)setUpView
{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -X_SCREEN_HEIGHT, X_SCREEN_WIDTH, X_SCREEN_HEIGHT * 5)];
_tableView.contentInset = UIEdgeInsetsMake(X_SCREEN_HEIGHT, 0, X_SCREEN_HEIGHT * 3, 0);
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.view addSubview:self.tableView];
[self.tableView reloadData];
if (kDataSourceCount != 0)
{
NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
[self.tableView scrollToRowAtIndexPath:curIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
[self addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:nil];
}
});
}
#pragma mark -
#pragma mark ----- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return kDataSourceCount;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:@"TeamListCell"];
if (!currentCell) {
currentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TeamListCell"];
}
currentCell.textLabel.text = [NSString stringWithFormat:@"%@ %ld", @"这个是:", indexPath.row];
currentCell.textLabel.textColor = [UIColor whiteColor];
currentCell.contentView.backgroundColor = [UIColor colorWithHexString:@"#131428" alpha:1.];
currentCell.backgroundColor = [UIColor xpf_randomColor];
currentCell.contentView.backgroundColor = [UIColor xpf_randomColor];
return currentCell;
}
#pragma mark -
#pragma mark - ScrollView delegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (kDataSourceCount != 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
CGPoint translatedPoint = [scrollView.panGestureRecognizer translationInView:scrollView];
//UITableView禁止响应其他滑动手势
scrollView.panGestureRecognizer.enabled = NO;
if(translatedPoint.y < -50 && self.currentIndex < (kDataSourceCount - 1)) {
self.currentIndex ++; //向下滑动索引递增
}
if(translatedPoint.y > 50 && self.currentIndex > 0) {
self.currentIndex --; //向上滑动索引递减
}
[UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//UITableView滑动到指定cell
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
} completion:^(BOOL finished) {
// UITableView可以响应其他滑动手势
scrollView.panGestureRecognizer.enabled = YES;
}];
});
}
}
#pragma mark -
#pragma mark -
@end