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];
    }
  • 相关阅读:
    Codeforces 791B. Bear and Friendship Condition 联通快 完全图
    SHU oj 422 风力观测 线段树
    hdu 5833 Zhu and 772002 高斯消元
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分
    Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
    Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
    hdu 3864 D_num Pollard_rho算法和Miller_Rabin算法
    hdu 3861 The King’s Problem trajan缩点+二分图匹配
    Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5776081.html
Copyright © 2011-2022 走看看