zoukankan      html  css  js  c++  java
  • UIcollectionView 实现 轮番图

     UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew

     

    #import <UIKit/UIKit.h>
    
    @interface YJCycleView : UIView
    
    @property (nonatomic, strong) NSArray *cycleArr;
    @end
    #import "YJCycleView.h"
    #import "UIView+Extension.h"
    #import "YJCycleCollectionViewCell.h"
    
    static NSString *const cycleCell = @"cycleCell";
    @interface YJCycleView ()<UICollectionViewDelegate,UICollectionViewDataSource>
    @property (nonatomic, strong) NSTimer *timer;
    @property (nonatomic, strong) UIPageControl *pageControl;
    @property (nonatomic, strong) UICollectionView *collectionView;
    
    @end
    @implementation YJCycleView
    
    - (instancetype)initWithFrame:(CGRect)frame {
    
        if (self = [super initWithFrame:frame]) {
            
            [self initView];
        }
        return self;
    }
    
    - (void)setCycleArr:(NSArray *)cycleArr {
    
        _cycleArr = cycleArr;
        
        [self.collectionView reloadData];
        self.pageControl.numberOfPages = cycleArr.count;
        //*10向左滑多少个
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cycleArr.count * 10 inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        
        [self removeCycleTimer];
        [self addCycleTimer];
    
    }
    
    - (void)initView {
        self.autoresizingMask = UIViewAutoresizingNone;
        [self addSubview:self.collectionView];
        [self addSubview:self.pageControl];
    }
    
    - (UICollectionView *)collectionView {
    
        if (_collectionView == nil) {
            
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.minimumLineSpacing = 0;
            layout.minimumInteritemSpacing = 0;
            layout.itemSize = CGSizeMake(self.width, self.height);
            layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
            _collectionView.delegate = self;
            _collectionView.dataSource = self;
            _collectionView.showsHorizontalScrollIndicator = NO;
            _collectionView.showsVerticalScrollIndicator = NO;
            _collectionView.pagingEnabled = YES;
            [_collectionView registerNib:[UINib nibWithNibName:@"YJCycleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cycleCell];
        }
        
        return _collectionView;
    }
    
    - (UIPageControl *)pageControl {
    
        if (_pageControl == nil) {
            _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.width/2)-(self.cycleArr.count*10)/2, self.height-60, 10, 10)];
            _pageControl.currentPage = 0;
            [_pageControl setPageIndicatorTintColor:[UIColor whiteColor]];
            [_pageControl addTarget:self action:@selector(pageMove:) forControlEvents:UIControlEventValueChanged];
        }
        return _pageControl;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
        //*1000设置最大值
        return self.cycleArr.count*1000;
    }
    
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        YJCycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cycleCell forIndexPath:indexPath];
        
        if (self.cycleArr.count !=0) {
            cell.imageView.image = [UIImage imageNamed:self.cycleArr[indexPath.item % self.cycleArr.count]];
        }
        cell.backgroundColor = indexPath.item % 2 == 0 ? [UIColor redColor] : [UIColor yellowColor];
        return cell;
    }
    
    - (void)pageMove:(UIPageControl *)page {
        CGFloat currentOffSetX = self.collectionView.contentOffset.x;
        CGFloat offSetX = currentOffSetX + self.collectionView.width * self.pageControl.currentPage;
        
        [self.collectionView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
        [self removeCycleTimer];
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
        [self addCycleTimer];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGFloat ofSetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5;
        self.pageControl.currentPage = (int)(ofSetX / scrollView.bounds.size.width) % (self.cycleArr.count);
    }
    
    - (void)removeCycleTimer
    {
        // 移除定时器
        [self.timer invalidate];
        self.timer = nil;
    }
    
    - (void)addCycleTimer {
    
        self.timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES];
        
        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    
    - (void)scrollToNext {
        CGFloat currentOffSetX = self.collectionView.contentOffset.x;
        CGFloat offSetX = currentOffSetX + self.collectionView.width;
        
        [self.collectionView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
    }
  • 相关阅读:
    centos升级glibc(升级到 2.17版)
    访问zabbix首页无法正常登陆
    解决zbx的web界面zabbix服务器端运行中 显示为不(启动命令)
    4. 日期
    3. 页面滚动条位置恢复
    2. 添加版本号
    1. webpack 复制文件
    4. git log的常见用法
    3. git获取历史版本
    2. Git-命令行-删除本地和远程分支
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5776081.html
Copyright © 2011-2022 走看看