iOS用UICollectonview实现轮播图效果

来源:互联网 发布:香港域名注册有限公司 编辑:程序博客网 时间:2024/06/05 09:17

//

//  ViewController.m

//  UICollectionviewPlay

//

//  Created by clowire on 16/10/13.

//  Copyright © 2016 clowire. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong)UICollectionView * collectionView;

@property (nonatomic,strong)UIPageControl * pageControl;

@property (nonatomic,strong)NSTimer * timer;



- (void)initialUSerInterface;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    [selfinitialUSerInterface];

    // Do any additional setup after loading the view, typically from a nib.

}



//懒加载pageControl

- (UIPageControl *)pageControl{

    

    if (_pageControl ==nil) {

        //分页控件,本质上和scorllView没有任何关系,是2个独立的控件

        _pageControl = [[UIPageControlalloc]init];

        _pageControl.numberOfPages =5;

        CGSize size = [_pageControlsizeForNumberOfPages:5];

        

        _pageControl.bounds =CGRectMake(0,0, size.width, size.width);

        _pageControl.center =CGPointMake(self.view.center.x,130);

        _pageControl.pageIndicatorTintColor = [UIColorredColor];

        [self.viewaddSubview:_pageControl];

        

    }

    return_pageControl;

}



- (void)initialUSerInterface{

    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc]init];

    layout.itemSize =CGSizeMake(self.view.frame.size.width,250);

    layout.scrollDirection =UICollectionViewScrollDirectionHorizontal;

    

    

    UICollectionView *collectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0,10, self.view.frame.size.width,250) collectionViewLayout:layout];

    collectionView.backgroundColor = [UIColorwhiteColor];

    collectionView.delegate =self;

    collectionView.dataSource =self;

    [collectionView registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"myCell"];

    collectionView.pagingEnabled =YES;

    collectionView.showsHorizontalScrollIndicator =NO;

    collectionView.showsVerticalScrollIndicator =NO;

    collectionView.bounces =NO;

    collectionView.contentSize =CGSizeMake(5 * collectionView.bounds.size.width,0);

    [self.viewaddSubview:collectionView];

    

    self.collectionView = collectionView;

    

    [selfaddNSTime];

    self.pageControl.currentPage = 0;

}






//添加定时器

- (void)addNSTime{

    

    NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(nextPage)userInfo:nilrepeats:YES];

    //添加到runloop

    [[NSRunLoopmainRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];

    [timer fire];

    self.timer = timer;

}


//删除定时器

- (void)removeNSTimer{

    

    [self.timerinvalidate];

    self.timer =nil;

}


//自动滚动

- (void)nextPage{

    

    NSInteger currentNumber =self.pageControl.currentPage;

    CGFloat x = ((currentNumber +1)%5) *self.collectionView.bounds.size.width;

    

    if (currentNumber <=5) {

        [self.collectionViewsetContentOffset:CGPointMake(x,0) animated:YES];

        

    }else{

        [self.collectionViewsetContentOffset:CGPointMake(x,0) animated:NO];

    }

    

    self.pageControl.currentPage = x;

    

}







#pragma mark --)<UICollectionViewDataSource,UICollectionViewDelegate>



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    

    return1;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    

    return5;

    

}




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    UICollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"myCell"forIndexPath:indexPath];

    cell.backgroundColor = [UIColoryellowColor];

    

    

    UIImageView *image = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:[NSStringstringWithFormat:@"%ld",indexPath.row +1]]];

    image.frame = cell.bounds;

    [cell addSubview:image];

    

    return cell;

    

}



//当用户开始拖拽的时候就调用

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    

    [selfremoveNSTimer];

}



//当用户停止拖拽的时候调用

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    

    [selfaddNSTime];

}


//设置页码

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

    

    int page = (int)(scrollView.contentOffset.x/scrollView.frame.size.width + 0.5)%5;

    

    self.pageControl.currentPage = page;

    

}- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



0 0