iOS实现轮播图banner示例
楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片所以用了SDWebImage这个三方库当然自己也可以去掉
类型后面有*号如用使用请自行加上。。。。。
代码:.h文件
@protocolTJXViewDelegate<NSObject> //判断点击的那个 -(void)sendImageName:(TJXView*)TJXViewandName:(NSInteger)selectImage; @end @interfaceTJXView:UIView @property(nonatomic,weak)id<TJXViewDelegate>delegate; //传一个frame和装有图片名字的数组过来 //参数一:frame //参数二:装有图片名字的数组 //参数三:BOOL如果是YES,那么自动滚动,如果是NO不滚动 -(id)initWithFrame:(CGRect)frameandImageNameArray: (NSMutableArray*)imageNameArrayandIsRunning:(BOOL)isRunning; @end
.m文件
@interfaceTJXView()<UIScrollViewDelegate> { NSInteger_currentPage;//记录真实的页码数 NSTimer*_timer;//生命一个全局变量 } @property(nonatomic,assign)BOOLisRun; @property(nonatomic,strong)NSMutableArray*imageArray;//存储图片的名字 @property(nonatomic,strong)UIScrollView*scrollView; @property(nonatomic,strong)UIPageControl*pageControl; @property(nonatomic,assign)CGFloatwidth;//view的宽 @property(nonatomic,assign)CGFloatheight;//view的高 @end -(id)initWithFrame:(CGRect)frameandImageNameArray:(NSMutableArray*)imageNameArrayandIsRunning:(BOOL)isRunning{ self=[superinitWithFrame:frame]; if(self){ _width=self.frame.size.width; _height=self.frame.size.height; //arrayWithArray把数组中的内容放到一个数组中返回 self.imageArray=[NSMutableArrayarrayWithArray:imageNameArray]; //在数组的尾部添加原数组第一个元素 [self.imageArrayaddObject:[imageNameArrayfirstObject]]; //在数组的首部添加原数组最后一个元素 [self.imageArrayinsertObject:[imageNameArraylastObject]atIndex:0]; self.isRun=isRunning; _currentPage=0; [selfcreateSro]; [selfcreatePageControl]; [selfcreateTimer]; } returnself; } -(void)createTimer{ if(_isRun==YES){ _timer=[NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(change)userInfo:nilrepeats:YES]; [[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:NSRunLoopCommonModes];} } -(void)change{ //1获得当前的点 CGPointpoint=_scrollView.contentOffset; //2求得将要变换的点 CGPointendPoint=CGPointMake(point.x+_width,0); //判断 if(endPoint.x==(self.imageArray.count-1)*_width){ [UIViewanimateWithDuration:0.25animations:^{ _scrollView.contentOffset=CGPointMake(endPoint.x,0); }completion:^(BOOLfinished){ //动画完成的block _scrollView.contentOffset=CGPointMake(_width,0); CGPointrealEnd=_scrollView.contentOffset; //取一遍页码数 _currentPage=realEnd.x/_width; _pageControl.currentPage=_currentPage-1; }]; } else{ //0.25s中更改一个图片 [UIViewanimateWithDuration:0.25animations:^{ _scrollView.contentOffset=endPoint; }completion:^(BOOLfinished){ }]; CGPointrealEnd=_scrollView.contentOffset; //取一遍页码数 _currentPage=realEnd.x/_width; _pageControl.currentPage=_currentPage-1; } } //创建页码指示器 -(void)createPageControl{ _pageControl=[[UIPageControlalloc]initWithFrame:CGRectMake(_width-200,_height-30,100,30)]; _pageControl.centerX=_width/2; _pageControl.numberOfPages=self.imageArray.count-2; _pageControl.pageIndicatorTintColor=WP_GRAY_COLOR; _pageControl.currentPageIndicatorTintColor=[UIColorwhiteColor]; _pageControl.userInteractionEnabled=NO; [selfaddSubview:_pageControl]; } //创建滚动视图 -(void)createSro{ _scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,_width,_height)]; _scrollView.contentSize=CGSizeMake(_width*self.imageArray.count,_height); for(inti=0;i<self.imageArray.count;i++){ UIImageView*imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(i*_width,0,_width,_height)]; //imageView.image=[UIImageimageNamed:self.imageArray[i]]; [imageViewsd_setImageWithURL:self.imageArray[i]placeholderImage:[UIImageimageNamed:@"home_banner_blank"]]; imageView.userInteractionEnabled=YES; imageView.tag=200+i; UITapGestureRecognizer*tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)]; [imageViewaddGestureRecognizer:tap]; [_scrollViewaddSubview:imageView]; } //水平指示条不显示 _scrollView.showsHorizontalScrollIndicator=NO; //关闭弹簧效果 _scrollView.bounces=NO; //设置用户看到第一张 _scrollView.contentOffset=CGPointMake(_width,0); //设置代理 _scrollView.delegate=self; //分页效果 _scrollView.pagingEnabled=YES; [selfaddSubview:_scrollView]; } -(void)tap:(UITapGestureRecognizer*)tap{ if(_delegate&&[_delegaterespondsToSelector:@selector(sendImageName:andName:)]){ [_delegatesendImageName:selfandName:tap.view.tag-201]; }else{ NSLog(@"没有设置代理或者没有事先协议的方法"); } } #pragmamarkUIScrollViewDelegate //停止滚动 -(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{ if(_timer){ [_timersetFireDate:[NSDatedateWithTimeIntervalSinceNow:2]]; } //图片的个数12345678 //真实的页码01234567 //显示的页码012345 CGPointpoint=_scrollView.contentOffset; if(point.x==(self.imageArray.count-1)*_width){ scrollView.contentOffset=CGPointMake(_width,0); } if(point.x==0){ scrollView.contentOffset=CGPointMake((self.imageArray.count-2)*_width,0); } //取一遍页码数 CGPointendPoint=scrollView.contentOffset; _currentPage=endPoint.x/_width; _pageControl.currentPage=_currentPage-1; } //手指开始触摸的时候,停止计时器 -(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{ if(_timer){ //如果有,停掉 [_timersetFireDate:[NSDatedistantFuture]]; } }
在项目中 导入头文件 遵守代理
TJXView*TJXView=[[TJXViewalloc]initWithFrame:CGRectMake(0,0,WPSCREEN_WIDTH,100*WPSCREEN_HIGTH_RATIO)andImageNameArray:self.bannerImagerandIsRunning:YES]; TJXView.delegate=self; [self.viewaddSubview:TJXView]; #pragmamarkTJXViewDelegate -(void)sendImageName:(TJXView*)TJXViewandName:(NSInteger)selectImage{ KKLog(@"%ld",(long)selectImage); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。