轮播图为一种常见的方式,常用于各种网站,或者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 }
效果图: