iOS最笨的办法实现无限轮播图(网络加载)

简单的做了一下:

使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 
可以使用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

.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

iOS最笨的办法实现无限轮播图(网络加载)的更多相关文章

  1. iOS 两种不同的图片无限轮播

    代码地址如下:http://www.demodashi.com/demo/11608.html 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵, ...

  2. 用UICollectionView实现无限轮播图

    用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节

  3. 简单介绍无限轮播图,js源代码

    无限轮播图js源代码,今天介绍一下用js简单的编写无限轮播图 <!DOCTYPE html> <html>   <head>     <meta charse ...

  4. Android之无限轮播图源代码

    Android轮播广告图是大家经常用到的一个控件今天便撸了一把代码 实现步骤 使用Viewpager进行实现图片滑动 设置ViewPager的数据,让其无限切换 Activity代码 public c ...

  5. iOS开发之 用第三方类库实现轮播图

    在github上面有很多的第三方类库,大大节约了大家的开发时间 下载地址:https://github.com/gsdios/SDCycleScrollView 现已支持cocoapods导入:pod ...

  6. swift-自定义无限轮播图

    一  前言 1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift. 2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正. 3.我决定一步步 ...

  7. 使用FlaycoBanner实现图片轮播效果(加载网络图片)

    FlaycoBanner是一个开源图片轮播框架,支持android2.2及以上: git地址:https://github.com/H07000223/FlycoBanner_Master 在andr ...

  8. iOS 无限轮播图的两种实现

    首先说一下实现的思想: 用UIScrollView实现,在scrollView上添加3个UIImageView,分别用来显示上一张图片,当前显示的图片,下一张图片.scrollView在不滑动的时候永 ...

  9. 自定义完美的ViewPager 真正无限循环的轮播图

    网上80%的思路关于Android轮播图无限循环都是不正确的,不是真正意义上的无限循环, 其思路大多是将ViewPager的getCount方法返回值设置为Integer.MAX_VALUE, 然后呢 ...

随机推荐

  1. The Art of Mocking

    One of the challenges developers face when writing unit tests is how to handle external dependencies ...

  2. mapx 32位在win8 64位上使用

    在可以安装32位mapx的电脑上安装并破解后,将安装文件复制出来,放到c盘根目录下,用下面语句进行注册即可 Regsvr32 C:\MapX\Mapx50.DLL Regsvr32 C:\\MapX\ ...

  3. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  4. vi中使用“/”查找字符

    在vi 文件中使用"/"查找字符串 命令模式下,输入 /word 后回车,即查找word,按 n 查找下一个匹配单词,按 N 查找上一个匹配单词.

  5. pip virtualenv requirements

    pip可以很方便的安装.卸载和管理Python的包.virtualenv则可以建立多个独立的虚拟环境,各个环境中拥有自己的python解释器和各自的package包,互不影响.pip和virtuale ...

  6. 【Android高级】NDK/JNI编程技术基础介绍

    作为一个Andoird的Java程序猿,会受到Java语言的局限.由于作为一面门向对象的语言不能像C/C++那样轻易调用与硬件有关的操作.因此JNI就搭建了这样一个桥梁,使Java和C/C++语言之间 ...

  7. json-server模拟接口获取mock数据

    转载:http://blog.csdn.net/stevennest/article/details/76167343 安装json-server 运行以下命令 cnpm install json-s ...

  8. inspect模块详解

    inspect模块主要提供了四种用处: (1).对是否是模块,框架,函数等进行类型检查. (2).获取源码 (3).获取类或函数的参数的信息 (4).解析堆栈 使用inspect模块可以提供自省功能, ...

  9. [经验总结]material design效果与开发总结

    首先贴一个參考过的文章,写的不错: 在低版本号android系统上实现Material design应用 以下是工作中总结出来的,列出了在<5.0的设备是怎样实现material design的 ...

  10. 【Excle数据透视表】如何利用图标集将销售数据划分为五个等级

    我们如何用图标集来直观看出订单情况呢? 现在有数据如下: 步骤 选中"订单列"→开始→条件格式→图标集→等级→ 此时,在每个数字前面都出现了一个等级符号了 查看预置五等级图标集的规 ...