效果圖
源代碼
NYCarouselView.h
//
// NYCarouselView.h
// 廣告輪播CollectionView
//
// Created by IOS on 15/12/26.
// Copyright ? 2015年 com.itcat.com. All rights reserved.
//
#import
@interface NYCarouselView : UICollectionView
/**
* 啟動時鐘
*/
-(void)startTimer;
/**
* 停止時鐘
*/
-(void)updateTimer;
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames;
@property (nonatomic, strong) NSArray *imageNames;
/**
* 每個輪播圖片的點擊事件
*/
@property (nonatomic, copy) void(^cellDidSelectItemAtIndexPath)(UICollectionView *collection,NSIndexPath *indexPath);
@end
NYCarouselView.m
//
// NYCarouselView.m
// 廣告輪播CollectionView
//
// Created by IOS on 15/12/26.
// Copyright ? 2015年 com.itcat.com. All rights reserved.
//
#import "NYCarouselView.h"
@interface NYCarouselView()
@property (nonatomic, weak) UIPageControl *carouselPageControl;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation NYCarouselView
static NSString * const carouselID = @"NYCarouselView";
-(instancetype)initWithFrame:(CGRect)frame
{
UICollectionViewFlowLayout *carouseLayout = [[UICollectionViewFlowLayout alloc]init];
carouseLayout.itemSize = frame.size;// 設置cell的尺寸
carouseLayout.minimumLineSpacing = 0;// 清空行距
carouseLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 設置滾動的方向
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:carouselID];
self.dataSource = self;
self.delegate = self;
[self startTimer];
return [super initWithFrame:frame collectionViewLayout:carouseLayout];
}
-(instancetype)initWithFrame:(CGRect)frame imageNames:(NSArray *)imageNames
{
self.imageNames = imageNames;
[self scrollViewDidScroll:self];
return [self initWithFrame:frame];
}
-(void)layoutSubviews{
[super layoutSubviews];
self.bounces = NO;//去掉彈簧效果
self.showsHorizontalScrollIndicator = NO;//去掉水平顯示的拖拽線
self.pagingEnabled = YES;//分頁效果
}
#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imageNames.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:carouselID forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.imageNames[indexPath.row]]];
cell.backgroundView = imageView;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.cellDidSelectItemAtIndexPath(collectionView,indexPath);
}
#pragma mark - 分頁
//分頁控件的監聽方法
-(void)pageChanged:(UIPageControl *)page
{
[self.timer invalidate];
//根據頁數,調整滾動視圖中得圖片位置contentOffset
CGFloat x = page.currentPage * self.bounds.size.width;
[self setContentOffset:CGPointMake(x, 0) animated:YES];
[self startTimer];
}
#pragma mark - 時鐘
/**啟動時鐘*/
-(void)startTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
//添加運行循環
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)updateTimer
{
//頁號發生變化
//(當前頁數 + 1) % 總頁數
NSUInteger count = self.imageNames.count;
if (count == 0) {
NSLog(@"圖片個數是0");
return;
}
int page = (self.carouselPageControl.currentPage+1) % (int)count;
self.carouselPageControl.currentPage = page;
//調用監聽方法。讓滾動視圖滾動
[self pageChanged:self.carouselPageControl];
}
/**
抓住圖片時,停止時鐘,松手後,開啟時鐘
*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//停止時鐘,停止後就不能在使用,如果要啟用時鐘,需要重新實例化
[self.timer invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//啟動時鐘
[self startTimer];
}
#pragma mark - UIScrollView代理
// 只要一滾動就會調用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// NSLog(scrollView.);
// 獲取當前的偏移量,計算當前第幾頁
int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
// 設置頁數
self.carouselPageControl.currentPage = page;
}
#pragma mark - 懶加載
-(UIPageControl *)carouselPageControl
{
if (_carouselPageControl == nil) {
UIPageControl * carouselPageControl = [[UIPageControl alloc]init];
//總頁數
carouselPageControl.numberOfPages = self.imageNames.count;
//控件尺寸
CGSize size = [_carouselPageControl sizeForNumberOfPages:self.imageNames.count];
carouselPageControl.bounds = CGRectMake(0, 0, size.width, size.height);
//pageControl的位置
carouselPageControl.center = CGPointMake(self.center.x, self.bounds.size.height * 0.85);
//設置顏色
carouselPageControl.pageIndicatorTintColor = [UIColor redColor];
carouselPageControl.currentPageIndicatorTintColor = [UIColor blackColor];
//添加監聽方法
[carouselPageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
[self.window addSubview:carouselPageControl];
_carouselPageControl = carouselPageControl;
}
return _carouselPageControl;
}
-(NSArray *)imageNames{
if (_imageNames == nil) {
_imageNames = [NSArray array];
}
return _imageNames;
}
@end