UIScrollView 分页滚动,简单代码 tool

UIScrollView 分页滚动,简单代码 tool,第1张

1、创建ScrollerPageTool 工具

//
//  ScrollerPageTool.m
//  CIC
//
//  Created by lgy on 2022/1/22.
//

#import "ScrollerPageTool.h"


@implementation ScrollerPageTool


/// 设置监听滑动的方向
/// @param scrollView  滑动的scrollView
- (void) setScrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.isTracking) {
        //判断左右滑动时
         if (scrollView.contentOffset.x < self.lastContentOffset ){
             //向右
             self.scrollDirectionLeft = YES;
         } else if (scrollView. contentOffset.x > self.lastContentOffset ){
             //向左
             self.scrollDirectionLeft = NO;
         }
        self.lastContentOffset = scrollView.contentOffset.x;
    }
}


/// 设置开始惯性滑动时的处理方式
/// @param scrollView scrollView
- (void) setScrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    [self autoScroller:scrollView];
}


/// 设置手指滑动结束后的处理方式
/// @param scrollView scrollView
/// @param decelerate 是否惯性滑动
- (void) setSrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //自动
    if (!decelerate) {
        [self autoScroller:scrollView];
    }
}


/// 滑动到自定位置
/// @param scrollView 滑动的UIScrollView
-(void) autoScroller:(UIScrollView *)scrollView{
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat pageWidth = self.pageWidth;
        NSInteger index = scrollView.contentOffset.x / self.pageWidth;
        CGRect frame = scrollView.frame;
        if (self.scrollDirectionLeft) {
            [scrollView scrollRectToVisible:CGRectMake(index*pageWidth, 0, frame.size.width, frame.size.height) animated:true];
        }else {
            [scrollView scrollRectToVisible:CGRectMake((index+1)*pageWidth, 0, frame.size.width, frame.size.height) animated:true];
        }
    });
}


@end

2、在 ScrolleView中 使用 ScrollerPageTool:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%.5lf",powf(1.05, 400));
    NSLog(@"%.5lf",powf(2, 16));
    [self.view addSubview:self.coView];
    
    self.pageTool = [ScrollerPageTool new];
    self.pageTool.pageWidth = 320+10;
    
//    self.coView.scrollEnabled = false;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.pageTool setScrollViewDidScroll:scrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    [self.pageTool setScrollViewWillBeginDecelerating:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.pageTool setSrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

ScrollerPageTool.h

//
//  ScrollerPageTool.h
//  CIC
//
//  Created by lgy on 2022/1/22.
//

#import 
#import 

NS_ASSUME_NONNULL_BEGIN




@interface ScrollerPageTool : NSObject

@property(nonatomic,assign) BOOL scrollDirectionLeft;
@property (nonatomic,assign) CGFloat lastContentOffset;
@property (nonatomic,assign) CGFloat pageWidth;


/// 设置监听滑动的方向
/// @param scrollView  滑动的scrollView
- (void) setScrollViewDidScroll:(UIScrollView *)scrollView;

/// 设置开始惯性滑动时的处理方式
/// @param scrollView scrollView
- (void) setScrollViewWillBeginDecelerating:(UIScrollView *)scrollView;

/// 设置手指滑动结束后的处理方式
/// @param scrollView scrollView
/// @param decelerate 是否惯性滑动
- (void) setSrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

@end

NS_ASSUME_NONNULL_END

viewController

//
//  ViewController.m
//  CIC
//
//  Created by lgy on 2021/12/22.
//

#import "ViewController.h"
#import "ScrollerPageTool.h"

@interface ViewController ()



@property (nonatomic,strong) UICollectionView *coView;

@property (nonatomic,strong) ScrollerPageTool *pageTool;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%.5lf",powf(1.05, 400));
    NSLog(@"%.5lf",powf(2, 16));
    [self.view addSubview:self.coView];
    
    self.pageTool = [ScrollerPageTool new];
    self.pageTool.pageWidth = 320+10;
    
//    self.coView.scrollEnabled = false;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.pageTool setScrollViewDidScroll:scrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    [self.pageTool setScrollViewWillBeginDecelerating:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.pageTool setSrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(320,200);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 18;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row % 9 == 0) {
        cell.backgroundColor = UIColor.brownColor;
    }else if (indexPath.row % 9 == 1) {
        cell.backgroundColor = UIColor.blueColor;
    }else if (indexPath.row % 9 == 2) {
        cell.backgroundColor = UIColor.grayColor;
    }else if (indexPath.row % 9 == 3) {
        cell.backgroundColor = UIColor.yellowColor;
    }else if (indexPath.row % 9 == 4) {
        cell.backgroundColor = UIColor.greenColor;
    }else if (indexPath.row % 9 == 5) {
        cell.backgroundColor = UIColor.redColor;
    }else if (indexPath.row % 9 == 6) {
        cell.backgroundColor = UIColor.blackColor;
    }else if (indexPath.row % 9 == 7) {
        cell.backgroundColor = UIColor.cyanColor;
    }else if (indexPath.row % 9 == 8) {
        cell.backgroundColor = UIColor.darkGrayColor;
    }
    
    return cell;
}

-(UICollectionView *) coView{
    if (!_coView) {
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 10;
        _coView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 200) collectionViewLayout:flowLayout];
        _coView.dataSource = self;
        _coView.delegate = self;
        _coView.bounces = false;
        _coView.showsVerticalScrollIndicator = NO;
        _coView.showsHorizontalScrollIndicator = NO;
        //_coView.pagingEnabled = YES;
        _coView.backgroundColor = [UIColor clearColor];
        [_coView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    }
    return _coView;
}


@end

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993619.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存