輪播圖為一種常見的方式,常用于各種網站,或者App中,當然,作為APP的啟動視圖也是不錯的選擇。
閑時封裝了一個,僅供新手參考。
1.新建工程,建立輪播圖類
建立一個空的工程,新建一個類,起名為Carousel,繼承于UIView
2.編寫Carousel類接口
1 @interface Carousel : UIView 2 typedef NS_ENUM(NSInteger,UICarouselPageType){ 3 //建立一個枚舉型,來設置Carousel的樣式 4 UICarouselPageTypeCenter,//設置pageControl在中心 5 UICarouselPageTypeLeft, //設置pageControl在左側 6 UICarouselPageTypeRight, //設置pageControl在右側 7 }; 8 @property(nonatomic,strong)NSArray *ImageArry;//用于接收來自外部的圖片 9 @property(nonatomic,assign)NSTimeInterval duration;//用于接收每張圖片的持續時間 10 @property(nonatomic,assign)UICarouselPageType PageType; 11 @end
3.內部代碼
1.用懶加載的方式定義UIScrollView和UIPageControl
? (1)為Carousel類建立延展
1 @interface Carousel()<UIScrollViewDelegate> 2 @property(nonatomic,strong)UIScrollView *scroll; 3 @property(nonatomic,strong)UIPageControl *pageControl; 4 @property(nonatomic,assign)int index; 5 @property(nonatomic,strong)NSTimer *timer; 6 @end
?
?
?(2)重寫初始化方法
1 -(instancetype)initWithFrame:(CGRect)frame{ 2 self =[super initWithFrame:frame]; 3 if (self) { 4 _timer =[[NSTimer alloc]init]; 5 _index=0; 6 } 7 return self; 8 }
?
(3)scrollView
1 -(UIScrollView *)scroll{ 2 if (_scroll==nil) { 3 _scroll =[[UIScrollView alloc]initWithFrame:self.bounds]; 4 _scroll.delegate=self; 5 _scroll.contentSize=CGSizeMake([_ImageArry count]*WIDTH,HEIGHT); 6 _scroll.pagingEnabled=YES;//允許整頁翻動 7 _scroll.bounces=NO; 8 _scroll.showsHorizontalScrollIndicator=NO; 9 _scroll.showsVerticalScrollIndicator=NO; 10 for (int i=0; i<[_ImageArry count]; i++) { 11 UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, HEIGHT)]; 12 UIImage *image=[_ImageArry objectAtIndex:i]; 13 imageView.image=image; 14 [_scroll addSubview:imageView]; 15 } 16 } 17 return _scroll; 18 }
?
?(4)pageControl
1 -(UIPageControl *)pageControl{ 2 if (_pageControl ==nil) { 3 _pageControl =[[UIPageControl alloc]init]; 4 [self setPageControlFrame]; 5 _pageControl.numberOfPages=[_ImageArry count]; 6 _pageControl.pageIndicatorTintColor=[UIColor greenColor]; 7 _pageControl.currentPageIndicatorTintColor=[UIColor redColor]; 8 [_pageControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged]; 9 } 10 return _pageControl; 11 } 12 -(void)setPageControlFrame{ 13 //當PageType有不同值的時候 有不同的frame 14 if (_PageType==UICarouselPageTypeLeft) { 15 _pageControl.frame=CGRectMake(0,HEIGHT-20,150,20); 16 }else if (_PageType ==UICarouselPageTypeRight){ 17 _pageControl.frame=CGRectMake(WIDTH-150,HEIGHT-20,150,20); 18 }else{ 19 _pageControl.frame=CGRectMake(WIDTH/2-75,HEIGHT-20,150,20); 20 } 21 } 22 -(void)change:(UIPageControl *)page{ 23 [_timer invalidate]; 24 _timer=nil; 25 _scroll.contentOffset=CGPointMake(page.currentPage*WIDTH, 0); 26 _index=(int)page.currentPage;//重新給index賦值 27 _timer =[NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(lunbo) userInfo:nil repeats:YES]; 28 }
?
?
(5) 重寫ImageArray的set方法
1 -(void)setImageArry:(NSArray *)ImageArry{ 2 //重寫Set方法 當ImageArray有值的時候顯示scrollView和PageControl 3 if (_ImageArry!=ImageArry) { 4 _ImageArry =ImageArry; 5 [self addSubview:self.scroll]; 6 [self addSubview:self.pageControl]; 7 _timer =[NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(lunbo) userInfo:nil repeats:YES]; 8 } 9 }
?
(6)Scroll的代理方法
1 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 2 //開始拖動時銷毀計時器 3 [_timer invalidate]; 4 _timer =nil; 5 } 6 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 7 //減速結束時重新生成計時器 8 _index=(int)_pageControl.currentPage;//重新給index賦值 9 _timer =[NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(lunbo) userInfo:nil repeats:YES]; 10 } 11 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 12 _pageControl.currentPage=scrollView.contentOffset.x/WIDTH; 13 }
?4.應用Carousel類
1 #import "ViewController.h" 2 #import "Carousel.h" 3 @interface ViewController () 4 @end 5 @implementation ViewController 6 7 - (void)viewDidLoad { 8 [super viewDidLoad]; 9 Carousel *lunbo =[[Carousel alloc]initWithFrame:CGRectMake(0, 100, 414, 300)]; 10 NSMutableArray *arr=[[NSMutableArray alloc]initWithCapacity:9]; 11 for (int index=1; index<10; index++) { 12 UIImage *image =[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",index]]; 13 [arr addObject:image]; 14 } 15 lunbo.duration=1; 16 lunbo.PageType=UICarouselPageTypeCenter; 17 lunbo.ImageArry=arr; 18 [self.view addSubview:lunbo]; 19 // Do any additional setup after loading the view, typically from a nib. 20 }
?
效果圖:
?
?