iOS最笨的办法实现无限轮播图(网络加载)
来源:互联网 发布:virtuixomni淘宝 编辑:程序博客网 时间:2024/05/17 09:47
简单的做了一下:
使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行
可以使用SDWebImage(我就是用的这个)等。。需要自己导入并引用,然后修改部分代码
.h文件
// ScrollViewTimerView.h// ScrollViewTimer//// Created by 郑鹏 on 2016/12/9.// Copyright © 2016年 郑鹏. All rights reserved.//#import <UIKit/UIKit.h>@protocol ScrollViewTimerViewDelegate <NSObject>- (void)didSelectScrollViewWithSelectIndex:(NSInteger)selectIndex;@end@interface ScrollViewTimerView : UIView@property (nonatomic, weak) id<ScrollViewTimerViewDelegate> littleSunDelegate;- (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration;@property (nonatomic, strong) NSMutableArray * wheelImgArray;//轮播图 数组//是否需要@property (nonatomic, assign) BOOL isHidePageControl;@end
.m文件
//// ScrollViewTimerView.m// ScrollViewTimer//// Created by 郑鹏 on 2016/12/9.// Copyright © 2016年 郑鹏. All rights reserved.//#import "ScrollViewTimerView.h"#import "UIImageView+WebCache.h"@interface ScrollViewTimerView ()<UIScrollViewDelegate>{ NSTimer *_animationTimer; UIScrollView *_littleSunScrollView; UIPageControl *_pageControl;}@end@implementation ScrollViewTimerView- (instancetype)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration{ self = [super initWithFrame:frame]; _littleSunScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _littleSunScrollView.delegate = self; _littleSunScrollView.pagingEnabled = YES; _littleSunScrollView.showsHorizontalScrollIndicator = NO; _littleSunScrollView.showsVerticalScrollIndicator = NO; _littleSunScrollView.contentOffset = CGPointMake(self.frame.size.width, 0); [self addSubview:_littleSunScrollView]; _animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationDuration target:self selector:@selector(animationTimerDidFired:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:UITrackingRunLoopMode]; [self pauseTimer]; return self;}- (void)animationTimerDidFired:(NSTimer *)timer{ CGPoint newOffset = CGPointMake(_littleSunScrollView.contentOffset.x + CGRectGetWidth(self.frame), _littleSunScrollView.contentOffset.y); [_littleSunScrollView setContentOffset:newOffset animated:YES]; if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) { [self startTimerAfterTimeInterval:-2.0f]; [_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO]; }}#pragma mark- UIScrollViewDelegate- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self pauseTimer];}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self startTimerAfterTimeInterval:2.0f];}-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (_littleSunScrollView.contentOffset.x >= (_wheelImgArray.count+1)*self.frame.size.width) { [_littleSunScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO]; }else if (_littleSunScrollView.contentOffset.x <= 0) { [_littleSunScrollView setContentOffset:CGPointMake(_wheelImgArray.count*self.frame.size.width, 0) animated:NO]; } if (!_isHidePageControl) { _pageControl.currentPage = [self getCurrentPageBy:scrollView.contentOffset.x]; }}//公开的属性设置- (void)setWheelImgArray:(NSMutableArray *)wheelImgArray{ if (_wheelImgArray != wheelImgArray) { _wheelImgArray = wheelImgArray; _littleSunScrollView.contentSize = CGSizeMake(self.frame.size.width * (wheelImgArray.count + 2), self.frame.size.height); for (NSInteger i = 0; i < wheelImgArray.count+2; i++) { if (i == 0) { [self creatImgViewWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + wheelImgArray.count - 1 WithImgName:wheelImgArray[wheelImgArray.count - 1]]; }else if(i != wheelImgArray.count+1){ [self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 + i-1 WithImgName:wheelImgArray[i-1]]; }else if(i == wheelImgArray.count+1){ [self creatImgViewWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height) WithTag:20161212 WithImgName:wheelImgArray[0]]; } } _pageControl = [[UIPageControl alloc] init]; _pageControl.numberOfPages = _wheelImgArray.count; _pageControl.frame = CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 10); _pageControl.currentPage = 0; _pageControl.userInteractionEnabled = NO; [_pageControl setPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:0.5]]; [_pageControl setCurrentPageIndicatorTintColor:[UIColor colorWithRed:255.0 / 255.0 green:255.0 / 255.0 blue:255.0 / 255.0 alpha:1]]; [self addSubview:_pageControl]; [self startTimerAfterTimeInterval:2.0f]; }}- (void)creatImgViewWithFrame:(CGRect)frame WithTag:(NSInteger)tag WithImgName:(NSString *)imgName{ UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame]; imgView.userInteractionEnabled = YES; imgView.contentMode = UIViewContentModeScaleAspectFill; imgView.tag = tag; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickItem:)]; [imgView addGestureRecognizer:tap]; NSURL *imgUrl = [NSURL URLWithString:imgName]; [imgView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"jiazai"]]; [_littleSunScrollView addSubview:imgView];}-(void)setisHidePageControl:(BOOL)isHidePageControl{ if (_isHidePageControl != isHidePageControl) { _isHidePageControl = isHidePageControl; if (_isHidePageControl) { [_pageControl removeFromSuperview]; } }}//通过 计算 获取当前页- (NSInteger)getCurrentPageBy:(CGFloat)scrollViewContentOfX{ if (scrollViewContentOfX < self.frame.size.width) { return _wheelImgArray.count - 1; }else if (scrollViewContentOfX >= self.frame.size.width && scrollViewContentOfX < (_wheelImgArray.count+1)*self.frame.size.width) { return scrollViewContentOfX/self.frame.size.width - 1; }else{ return 0; }}//_animationTimer暂停-(void)pauseTimer{ if (![_animationTimer isValid]) { return ; } [_animationTimer setFireDate:[NSDate distantFuture]];}//_animationTimer开始-(void)startTimer{ if (![_animationTimer isValid]) { return ; } [_animationTimer setFireDate:[NSDate date]];}//_animationTimer在多久后开始- (void)startTimerAfterTimeInterval:(NSTimeInterval)interval{ if (![_animationTimer isValid]) { return ; } [_animationTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];}#pragma mark- 实现代理- (void)clickItem:(UITapGestureRecognizer *)tap{ UIView *view = tap.view; if ([self.littleSunDelegate respondsToSelector:@selector(didSelectScrollViewWithSelectIndex:)]) { [self.littleSunDelegate didSelectScrollViewWithSelectIndex:view.tag - 20161212]; }}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/@end
0 0
- iOS最笨的办法实现无限轮播图(网络加载)
- iOS 无限轮播图的实现思路
- 网络加载图片的无限轮播
- 移动端,当滚动到最底端的时候,无限加载,怎么实现的?
- ViewPager网络加载图片并无限轮播实现
- 最简单的本地加载图片无限自动轮播
- 网络无限轮播的简单实现
- 网络获取图片实现无限轮播图
- iOS - 网络数据加载等待页面的简单实现
- iOS实现无限后台background的方法
- iOS实现无限后台background的方法
- IOS 利用UIScrollView实现无限轮播图
- iOS 投机流实现 无限轮播图
- iOS:UICollectionView实现无限轮播图(Swift3)
- iOS使用UIScrollView实现无限循环轮播图
- ios网络图片的加载
- 维持网络连接,心跳是最愚蠢的办法
- 用最简单的方法实现ViewPager无限循环滑动(跑马灯)效果
- mysql读取一张表中的字段循环插入另一张表
- CCF Z字形扫描
- EasyUI tab加载iframe高度不适应
- Summernote – 基于 Bootstrap 的文本编辑器
- 紫书动规 例题9-7 UVA
- iOS最笨的办法实现无限轮播图(网络加载)
- C#多态与虚方法
- LeetCode : Path Sum III
- EF 与 sql server 转到mysql
- Node.js的安装和配置
- 射频识别技术漫谈(20)——RC系列射频接口芯片
- 手机端调用相机上传图片
- 第十、Java的二维数组
- PTA 阶乘升级版