都是準(zhǔn)備好的內(nèi)容,需要的朋友可以參考下" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果

 更新時(shí)間:2024年06月27日 10:29:11   作者:劉小哈哈哈  
這篇文章主要介紹了iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果,整體思路是我們將tableView 的contentinset設(shè)置為上面一個(gè)屏幕的高度,下面一個(gè)屏幕的高度,左右為0,這樣保證我們滾動(dòng)過去的時(shí)候
都是準(zhǔn)備好的內(nèi)容,需要的朋友可以參考下

效果圖

請(qǐng)?zhí)砑訄D片描述

思路

整體上我們使用tableView實(shí)現(xiàn),為了預(yù)留內(nèi)容的緩沖,我們將tableView 的contentinset設(shè)置為上面一個(gè)屏幕的高度,下面一個(gè)屏幕的高度,左右為0,這樣保證我們滾動(dòng)過去的時(shí)候
都是準(zhǔn)備好的內(nèi)容
然后就是滑動(dòng)效果的實(shí)現(xiàn)了,主要就是我們?cè)趕crollViewWillEndDragging方法中獲取到停止拖動(dòng)(手指離開)時(shí)候的速度。 在scrollViewDidEndDragging 方法中
通過translationInView方法判斷當(dāng)前滑動(dòng)的方向,
然后剛才獲取到的速度就派上用場了,當(dāng)我們手指離開時(shí)候的速度大于0.4的時(shí)候,我們切換頁面的臨界偏移量就是8 ,否則臨界偏移量就是60, 同時(shí),通過
CGPoint translatedPoint = [scrollView.panGestureRecognizer translationInView:scrollView];判斷translatedPoint.y我們可以
判斷滾動(dòng)的方向,判斷出方向之后,
使用UIView animateWithDuration動(dòng)畫快速翻頁

代碼

//
//  ViewController.m
//  LBDouyinScroll
//
//  Created by mac on 2024/6/26.
//
#import "ViewController.h"
#import "DouyinScrollTableViewCell.h"
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) CGFloat velocity;
@property (nonatomic, strong) NSMutableArray *colorArray;
@end
@implementation ViewController
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.colorArray = [NSMutableArray array];
    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 = [[UIColor alloc]initWithRed:rr green:rg blue:rb alpha:1];
        [self.colorArray addObject:color];
    }
    [self.tableView reloadData];
    // Do any additional setup after loading the view.
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DouyinScrollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DouyinScrollTableViewCell class])];
    [cell updateWithColor:self.colorArray[indexPath.row]];
    //    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    //    cell.backgroundColor = self.colorArray[indexPath.row];
    //    if (!cell.contentView.backgroundColor) {
    //        cell.contentView.backgroundColor = self.colorArray[indexPath.row];
    //    }
    //    return cell;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenHeight;
}
#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 = [scrollView.panGestureRecognizer translationInView:scrollView];
        //UITableView禁止響應(yīng)其他滑動(dòng)手勢
        scrollView.panGestureRecognizer.enabled = NO;
        CGFloat translateCheck = 60;
        if (fabs(self.velocity) > 0.4) {
            translateCheck = 8;
        }
        if(translatedPoint.y < -translateCheck && self.currentIndex < 10) {
            self.currentIndex ++;   //向下滑動(dòng)索引遞增
        }
        if(translatedPoint.y > translateCheck && self.currentIndex > 0) {
            self.currentIndex --;   //向上滑動(dòng)索引遞減
        }
        if (self.currentIndex == 10) {
        } else {
            [UIView animateWithDuration:0.15
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseOut animations:^{
                //UITableView滑動(dòng)到指定cell
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            } completion:^(BOOL finished) {
                //UITableView可以響應(yīng)其他滑動(dòng)手勢
                scrollView.panGestureRecognizer.enabled = YES;
            }];
        }
    });
}
#pragma - private
- (void)animationToIndex:(NSInteger)index
{
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.tableView.contentOffset = CGPointMake(0, kScreenHeight * index);
    } completion:^(BOOL finished) {
    }];
}
#pragma mark - lazy load
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, - kScreenHeight, CGRectGetWidth(self.view.bounds), kScreenHeight * 3) style:UITableViewStylePlain];
        [_tableView registerClass:[DouyinScrollTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DouyinScrollTableViewCell class])];
        _tableView.rowHeight = kScreenHeight;
        _tableView.contentInset = UIEdgeInsetsMake(kScreenHeight , 0, kScreenHeight, 0);
        _tableView.estimatedRowHeight = kScreenHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor redColor];
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.decelerationRate = UIScrollViewDecelerationRateFast;
    }
    return _tableView;
}
@end

其中最關(guān)鍵的就是下面的

- (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 = [scrollView.panGestureRecognizer translationInView:scrollView];
        //UITableView禁止響應(yīng)其他滑動(dòng)手勢
        scrollView.panGestureRecognizer.enabled = NO;
        CGFloat translateCheck = 60;
        if (fabs(self.velocity) > 0.4) {
            translateCheck = 8;
        }
        if(translatedPoint.y < -translateCheck && self.currentIndex < 10) {
            self.currentIndex ++;   //向下滑動(dòng)索引遞增
        }
        if(translatedPoint.y > translateCheck && self.currentIndex > 0) {
            self.currentIndex --;   //向上滑動(dòng)索引遞減
        }
        if (self.currentIndex == 10) {
        } else {
            [UIView animateWithDuration:0.15
                                  delay:0.0
                                options:UIViewAnimationOptionCurveEaseOut animations:^{
                //UITableView滑動(dòng)到指定cell
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            } completion:^(BOOL finished) {
                //UITableView可以響應(yīng)其他滑動(dòng)手勢
                scrollView.panGestureRecognizer.enabled = YES;
            }];
        }
    });
}

demo: link

到此這篇關(guān)于iOS 實(shí)現(xiàn)類似抖音滾動(dòng)效果的文章就介紹到這了,更多相關(guān)iOS 抖音滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于UIControl控件實(shí)現(xiàn)ios點(diǎn)贊功能

    基于UIControl控件實(shí)現(xiàn)ios點(diǎn)贊功能

    在開發(fā)當(dāng)中,可能很多時(shí)候都需要做個(gè)點(diǎn)贊的需求,如果用按鈕實(shí)現(xiàn),按鈕作為一個(gè)系統(tǒng)復(fù)合控件,外部是一個(gè) View--》UIControl的容器,本文給大家分享一個(gè)基于UIControl控件實(shí)現(xiàn)ios點(diǎn)贊功能,需要的朋友可以參考下
    2015-09-09
  • 關(guān)于iOS自帶九宮格拼音鍵盤和Emoji表情之間的一些坑

    關(guān)于iOS自帶九宮格拼音鍵盤和Emoji表情之間的一些坑

    這篇文章主要給大家介紹了關(guān)于iOS自帶九宮格拼音鍵盤和Emoji表情之間的一些坑文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能

    iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能

    這篇文章主要為大家詳細(xì)介紹了iOS AVCaptureSession實(shí)現(xiàn)視頻錄制功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決

    iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決

    這篇文章主要介紹了iOS使用WKWebView加載HTML5不顯示屏幕寬度的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • iOS Crash文件分析方法匯總

    iOS Crash文件分析方法匯總

    今天跟大家一起聊聊iOSCrash文件的幾種分析方法,都是平時(shí)比較常用的,有需要的小伙伴可以參考下
    2017-11-11
  • iOS高仿微信表情輸入功能代碼分享

    iOS高仿微信表情輸入功能代碼分享

    最近項(xiàng)目需求,要實(shí)現(xiàn)一個(gè)類似微信的的表情輸入功能,今天小編抽空給大家分享iOS高仿微信表情輸入功能代碼,非常不錯(cuò),感興趣的朋友參考下吧
    2016-11-11
  • 淺談iOS推送證書生成pem文件(詳細(xì)生成過程)

    淺談iOS推送證書生成pem文件(詳細(xì)生成過程)

    這篇文章主要介紹了淺談iOS推送證書生成pem文件(詳細(xì)生成過程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • iOS中設(shè)置view圓角化的四種方法示例

    iOS中設(shè)置view圓角化的四種方法示例

    最近因?yàn)楣ぷ鞯脑?遇到view圓角優(yōu)化的問題,所以將實(shí)現(xiàn)的幾種方法總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于iOS中設(shè)置view圓角化的四種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • iOS Label實(shí)現(xiàn)文字漸變色效果

    iOS Label實(shí)現(xiàn)文字漸變色效果

    文字漸變色可以使整體的效果更上一個(gè)檔次,最近在開發(fā)中就遇到了這個(gè)需求,所以整理出來,下面這篇文章主要給大家介紹了關(guān)于iOS Label實(shí)現(xiàn)文字漸變色效果的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • iOS使用UIKeyInput自定義密碼輸入框的方法示例

    iOS使用UIKeyInput自定義密碼輸入框的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS如何使用UIKeyInput自定義密碼輸入框的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02

最新評(píng)論