iOS開發日記58-廣告輪播collectionView,58-collectionview
今天博主有一個利用collectionView製作廣告輪播圖的需求,遇到了一些困痛點,在此和大家分享,希望能夠共同進步.
製作廣告輪播圖較為簡單,但其中可用三種方法:
1.使用ScrollView製作,簡單明了
2.使用TableView製作,將tableview與cell同時改變transform,旋轉90°,實現輪播
3.使用CollectionView製作,推薦使用collectionView
代碼較為簡單,貼出來與大家共用,請各位看官自行研究
NYCarouselView.h
//// NYCarouselView.h// 廣告輪播CollectionView//// Created by IOS on 15/12/26.// Copyright © 2015年 com.itcat.com. All rights reserved.//#import <UIKit/UIKit.h>@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()<UICollectionViewDataSource,UICollectionViewDelegate>@property (nonatomic, weak) UIPageControl *carouselPageControl;@property (nonatomic, strong) NSTimer *timer;@end@implementation NYCarouselViewstatic 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