这里是效果图
参考抖音的滚动效果,需要我们在结束拖动的时候,动画设置偏移量
这里有一个注意点,由于我们是在拖动结束的时候,手动改变tableview的偏移量,
改变了tableView 自身原有的的滚动效果,所以我们
需要讲tableView 的frame的高度设置为三个cell的高度,然后,设置contentInset
的顶部和底部都是cell的高度,否则会导致我们滚动的过程中cell还没有加载出来
,展示成一片空白的效果
直接上代码
// // DouyinScrollViewController.m // TEXT // // Created by mac on 2024/4/28. // Copyright © 2024 刘博. All rights reserved. // #import "DouyinScrollViewController.h" #import "DouyinScrollTableViewCell.h" static CGFloat const height = 700; @interface DouyinScrollViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, assign) NSInteger currentIndex; @property (nonatomic, assign) CGFloat velocity; @property (nonatomic, strong) NSMutableArray *colorArray; @end @implementation DouyinScrollViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; self.colorArray = [NSMutableArray array]; for (int i = 0; i 0.4) { translateCheck = 8; } if(translatedPoint.y translateCheck && 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 - lazy load - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60 - height, CGRectGetWidth(self.view.bounds), height * 3) style:UITableViewStylePlain]; [_tableView registerClass:[DouyinScrollTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DouyinScrollTableViewCell class])]; _tableView.rowHeight = height; _tableView.contentInset = UIEdgeInsetsMake(height , 0, height, 0); _tableView.estimatedRowHeight = height; _tableView.delegate = self; _tableView.dataSource = self; _tableView.backgroundColor = [UIColor redColor]; _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _tableView.separatorInset = UIEdgeInsetsZero; } return _tableView; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...