iOS用UICollectonview实现轮播图效果

//

//  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 {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self initialUSerInterface];

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

}



//懒加载pageControl

- (UIPageControl *)pageControl{

    

    if (_pageControl == nil) {

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

        _pageControl = [[UIPageControl alloc]init];

        _pageControl.numberOfPages = 5;

        CGSize size = [_pageControl sizeForNumberOfPages:5];

        

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

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

        _pageControl.pageIndicatorTintColor = [UIColor redColor];

        [self.view addSubview:_pageControl];

        

    }

    return _pageControl;

}



- (void)initialUSerInterface{

    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

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

    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    

    

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

    collectionView.backgroundColor = [UIColor whiteColor];

    collectionView.delegate = self;

    collectionView.dataSource = self;

    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];

    collectionView.pagingEnabled = YES;

    collectionView.showsHorizontalScrollIndicator = NO;

    collectionView.showsVerticalScrollIndicator = NO;

    collectionView.bounces = NO;

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

    [self.view addSubview:collectionView];

    

    self.collectionView = collectionView;

    

    [self addNSTime];

    self.pageControl.currentPage = 0;

}






//添加定时器

- (void)addNSTime{

    

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];

    //添加到runloop

    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    [timer fire];

    self.timer = timer;

}


//删除定时器

- (void)removeNSTimer{

    

    [self.timer invalidate];

    self.timer = nil;

}


//自动滚动

- (void)nextPage{

    

    NSInteger currentNumber = self.pageControl.currentPage;

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

    

    if (currentNumber <= 5) {

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

        

    }else{

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

    }

    

    self.pageControl.currentPage = x;

    

}







#pragma mark --)<UICollectionViewDataSource,UICollectionViewDelegate>



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

    

    return 1;

}


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

    

    return 5;

    

}




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

    

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

    cell.backgroundColor = [UIColor yellowColor];

    

    

    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld",indexPath.row + 1]]];

    image.frame = cell.bounds;

    [cell addSubview:image];

    

    return cell;

    

}



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

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

    

    [self removeNSTimer];

}



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

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

    

    [self addNSTime];

}


//设置页码

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

    

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

    

    self.pageControl.currentPage = page;

    

}- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值