反转基因福娃 发表于 2024-10-24 20:54:16

iOS 实现类似抖音翻页滚动结果

这里是结果图
https://i-blog.csdnimg.cn/blog_migrate/6b3e8e0d8a813fa550b8e12daf8c4e7c.gif
参考抖音的滚动结果,需要我们在竣事拖动的时候,动画设置偏移量
这里有一个注意点,由于我们是在拖动竣事的时候,手动改变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 () <UITableViewDelegate, UITableViewDataSource>

@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 {
    ;
    ;
    self.colorArray = ;
    for (int i = 0; i < 10; i ++) {
      int r = arc4random() % 255;
      int g = arc4random() % 255;
      int b = arc4random() % 255;
      CGFloat rr = r / 255.0;
      CGFloat rg = g / 255.0;
      CGFloat rb = b / 255.0;
      UIColor *color = [initWithRed:rr green:rg blue:rb alpha:1];
      ;
    }
    ;
    // Do any additional setup after loading the view.
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DouyinScrollTableViewCell *cell = )];
    ];
    //    cell.textLabel.text = ;
    //    cell.backgroundColor = self.colorArray;
    //    if (!cell.contentView.backgroundColor) {
    //      cell.contentView.backgroundColor = self.colorArray;
    //    }
    //    return cell;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return height;
}

#pragma mark - scrolllVIewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    self.velocity = velocity.y;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    dispatch_async(dispatch_get_main_queue(), ^{
      CGPoint translatedPoint = ;
      //UITableView禁止响应其他滑动手势
      scrollView.panGestureRecognizer.enabled = NO;
      CGFloat translateCheck = 60;
      NSLog(@"哈哈哈哈获取停止拖动时候的速度%f", self.velocity);
      if (fabs(self.velocity) > 0.4) {
            translateCheck = 8;
      }
      
      
      if(translatedPoint.y < -translateCheck && self.currentIndex < 10) {
            self.currentIndex ++;   //向下滑动索引递增
      }
      if(translatedPoint.y > translateCheck && self.currentIndex > 0) {
            self.currentIndex --;   //向上滑动索引递减
      }
      [UIView animateWithDuration:0.15
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut animations:^{
            //UITableView滑动到指定cell
             atScrollPosition:UITableViewScrollPositionTop animated:NO];
      } completion:^(BOOL finished) {
            //UITableView可以响应其他滑动手势
            scrollView.panGestureRecognizer.enabled = YES;
      }];
      
    });
}

#pragma mark - lazy load

- (UITableView *)tableView
{
    if (!_tableView) {
      _tableView = [ initWithFrame:CGRectMake(0, 60 - height, CGRectGetWidth(self.view.bounds), height * 3) style:UITableViewStylePlain];
       forCellReuseIdentifier:NSStringFromClass()];
      _tableView.rowHeight = height;
      _tableView.contentInset = UIEdgeInsetsMake(height , 0, height, 0);
      _tableView.estimatedRowHeight = height;
      _tableView.delegate = self;
      _tableView.dataSource = self;
      _tableView.backgroundColor = ;
      _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 .
// Pass the selected object to the new view controller.
}
*/

@end


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: iOS 实现类似抖音翻页滚动结果