小编给大家分享一下iOS如何实现轮播图banner,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序设计、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了向阳免费建站欢迎大家使用!楼主项目中需要有一个轮播图,因为比较简单,就自己写了个,因为是从网上弄得图片 所以用了SDWebImage 这个三方库 当然自己也可以去掉
类型后面有*号 如用使用 请自行加上。。。。。
代码:.h 文件
@protocol TJXViewDelegate//判断点击的那个 -(void)sendImageName:(TJXView *)TJXView andName:(NSInteger)selectImage; @end @interface TJXView : UIView @property (nonatomic,weak)id delegate; //传一个frame 和 装有图片名字的数组过来 //参数一:frame //参数二:装有图片名字的数组 //参数三:BOOL如果是YES,那么自动滚动,如果是NO不滚动 -(id)initWithFrame:(CGRect)frame andImageNameArray: (NSMutableArray * )imageNameArray andIsRunning:(BOOL)isRunning; @end
.m文件
@interface TJXView(){ NSInteger _currentPage; //记录真实的页码数 NSTimer *_timer; //生命一个全局变量 } @property (nonatomic,assign) BOOL isRun; @property (nonatomic,strong) NSMutableArray *imageArray;//存储图片的名字 @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) UIPageControl *pageControl; @property (nonatomic,assign) CGFloat width;//view的宽 @property (nonatomic,assign) CGFloat height;//view的高 @end -(id)initWithFrame:(CGRect)frame andImageNameArray:(NSMutableArray *)imageNameArray andIsRunning:(BOOL)isRunning{ self = [super initWithFrame:frame]; if (self) { _width = self.frame.size.width; _height = self.frame.size.height; //arrayWithArray 把数组中的内容放到一个数组中返回 self.imageArray = [NSMutableArray arrayWithArray:imageNameArray]; //在数组的尾部添加原数组第一个元素 [self.imageArray addObject:[imageNameArray firstObject]]; //在数组的首部添加原数组最后一个元素 [self.imageArray insertObject:[imageNameArray lastObject] atIndex:0]; self.isRun = isRunning; _currentPage = 0; [self createSro]; [self createPageControl]; [self createTimer]; } return self; } -(void)createTimer{ if (_isRun == YES) { _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES ]; [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes]; } } -(void)change{ //1获得当前的点 CGPoint point = _scrollView.contentOffset; //2求得将要变换的点 CGPoint endPoint = CGPointMake(point.x+_width, 0); //判断 if (endPoint.x == (self.imageArray.count-1)*_width) { [UIView animateWithDuration:0.25 animations:^{ _scrollView.contentOffset = CGPointMake(endPoint.x, 0); } completion:^(BOOL finished) { //动画完成的block _scrollView.contentOffset = CGPointMake(_width, 0); CGPoint realEnd = _scrollView.contentOffset; //取一遍页码数 _currentPage = realEnd.x/_width; _pageControl.currentPage = _currentPage-1; }]; } else{ //0.25s中更改一个图片 [UIView animateWithDuration:0.25 animations:^{ _scrollView.contentOffset = endPoint; } completion:^(BOOL finished) { }]; CGPoint realEnd = _scrollView.contentOffset; //取一遍页码数 _currentPage = realEnd.x/_width; _pageControl.currentPage = _currentPage-1; } } //创建页码指示器 -(void)createPageControl{ _pageControl = [[UIPageControl alloc]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 = [UIColor whiteColor]; _pageControl.userInteractionEnabled = NO; [self addSubview:_pageControl]; } //创建滚动视图 -(void)createSro{ _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _width, _height)]; _scrollView.contentSize = CGSizeMake(_width*self.imageArray.count, _height); for (int i = 0; i < self.imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*_width, 0, _width, _height)]; // imageView.image = [UIImage imageNamed:self.imageArray[i]]; [imageView sd_setImageWithURL:self.imageArray[i] placeholderImage:[UIImage imageNamed:@"home_banner_blank"]]; imageView.userInteractionEnabled = YES; imageView.tag = 200+i; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]; [imageView addGestureRecognizer:tap]; [_scrollView addSubview:imageView]; } //水平指示条不显示 _scrollView.showsHorizontalScrollIndicator = NO; //关闭弹簧效果 _scrollView.bounces = NO; //设置用户看到第一张 _scrollView.contentOffset = CGPointMake(_width, 0); //设置代理 _scrollView.delegate = self; //分页效果 _scrollView.pagingEnabled = YES; [self addSubview:_scrollView]; } -(void)tap:(UITapGestureRecognizer *)tap{ if(_delegate&&[_delegate respondsToSelector:@selector(sendImageName:andName:)]){ [_delegate sendImageName:self andName:tap.view.tag-201]; }else{ NSLog(@"没有设置代理或者没有事先协议的方法"); } } #pragma mark UIScrollViewDelegate //停止滚动 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (_timer) { [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]]; } //图片的个数 1 2 3 4 5 6 7 8 //真实的页码 0 1 2 3 4 5 6 7 //显示的页码 0 1 2 3 4 5 CGPoint point = _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); } //取一遍页码数 CGPoint endPoint = scrollView.contentOffset; _currentPage = endPoint.x/_width; _pageControl.currentPage = _currentPage-1; } //手指开始触摸的时候,停止计时器 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ if (_timer) { //如果有,停掉 [_timer setFireDate:[NSDate distantFuture]]; } }
在项目中 导入头文件 遵守代理
TJXView * TJXView = [[TJXView alloc]initWithFrame:CGRectMake(0, 0, WPSCREEN_WIDTH, 100*WPSCREEN_HIGTH_RATIO) andImageNameArray:self.bannerImager andIsRunning:YES]; TJXView.delegate = self; [self.view addSubview: TJXView]; #pragma mark TJXViewDelegate -(void)sendImageName:(TJXView *) TJXView andName:(NSInteger)selectImage{ KKLog(@"%ld",(long)selectImage); }
以上是“iOS如何实现轮播图banner”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。