茶具网站模板,html5电影网站建设,个人网站建设优化,vi设计网站排行榜轮播图为一种常见的方式#xff0c;常用于各种网站#xff0c;或者App中#xff0c;当然#xff0c;作为APP的启动视图也是不错的选择。 闲时封装了一个#xff0c;仅供新手参考。 1.新建工程#xff0c;建立轮播图类 建立一个空的工程#xff0c;新建一个类#xff0c…轮播图为一种常见的方式常用于各种网站或者App中当然作为APP的启动视图也是不错的选择。 闲时封装了一个仅供新手参考。 1.新建工程建立轮播图类 建立一个空的工程新建一个类起名为Carousel继承于UIView 2.编写Carousel类接口 1 interface Carousel : UIView2 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 _index0;
6 }
7 return self;
8 } 3scrollView 1 -(UIScrollView *)scroll{2 if (_scrollnil) {3 _scroll [[UIScrollView alloc]initWithFrame:self.bounds];4 _scroll.delegateself;5 _scroll.contentSizeCGSizeMake([_ImageArry count]*WIDTH,HEIGHT);6 _scroll.pagingEnabledYES;//允许整页翻动7 _scroll.bouncesNO;8 _scroll.showsHorizontalScrollIndicatorNO;9 _scroll.showsVerticalScrollIndicatorNO;
10 for (int i0; i[_ImageArry count]; i) {
11 UIImageView *imageView [[UIImageView alloc]initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, HEIGHT)];
12 UIImage *image[_ImageArry objectAtIndex:i];
13 imageView.imageimage;
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 (_PageTypeUICarouselPageTypeLeft) {
15 _pageControl.frameCGRectMake(0,HEIGHT-20,150,20);
16 }else if (_PageType UICarouselPageTypeRight){
17 _pageControl.frameCGRectMake(WIDTH-150,HEIGHT-20,150,20);
18 }else{
19 _pageControl.frameCGRectMake(WIDTH/2-75,HEIGHT-20,150,20);
20 }
21 }
22 -(void)change:(UIPageControl *)page{
23 [_timer invalidate];
24 _timernil;
25 _scroll.contentOffsetCGPointMake(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.currentPagescrollView.contentOffset.x/WIDTH;
13 } 4.应用Carousel类 1 #import ViewController.h2 #import Carousel.h3 interface ViewController ()4 end5 implementation ViewController6 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 index1; index10; index) {
12 UIImage *image [UIImage imageNamed:[NSString stringWithFormat:%d.jpg,index]];
13 [arr addObject:image];
14 }
15 lunbo.duration1;
16 lunbo.PageTypeUICarouselPageTypeCenter;
17 lunbo.ImageArryarr;
18 [self.view addSubview:lunbo];
19 // Do any additional setup after loading the view, typically from a nib.
20 } 效果图 转载于:https://www.cnblogs.com/pangxuhui/p/5671957.html