iOS最笨的办法实现无限轮播图(网络加载)
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最笨的办法实现无限轮播图(网络加载)的更多相关文章
- iOS 两种不同的图片无限轮播
代码地址如下:http://www.demodashi.com/demo/11608.html 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵, ...
- 用UICollectionView实现无限轮播图
用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节
- 简单介绍无限轮播图,js源代码
无限轮播图js源代码,今天介绍一下用js简单的编写无限轮播图 <!DOCTYPE html> <html> <head> <meta charse ...
- Android之无限轮播图源代码
Android轮播广告图是大家经常用到的一个控件今天便撸了一把代码 实现步骤 使用Viewpager进行实现图片滑动 设置ViewPager的数据,让其无限切换 Activity代码 public c ...
- iOS开发之 用第三方类库实现轮播图
在github上面有很多的第三方类库,大大节约了大家的开发时间 下载地址:https://github.com/gsdios/SDCycleScrollView 现已支持cocoapods导入:pod ...
- swift-自定义无限轮播图
一 前言 1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift. 2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正. 3.我决定一步步 ...
- 使用FlaycoBanner实现图片轮播效果(加载网络图片)
FlaycoBanner是一个开源图片轮播框架,支持android2.2及以上: git地址:https://github.com/H07000223/FlycoBanner_Master 在andr ...
- iOS 无限轮播图的两种实现
首先说一下实现的思想: 用UIScrollView实现,在scrollView上添加3个UIImageView,分别用来显示上一张图片,当前显示的图片,下一张图片.scrollView在不滑动的时候永 ...
- 自定义完美的ViewPager 真正无限循环的轮播图
网上80%的思路关于Android轮播图无限循环都是不正确的,不是真正意义上的无限循环, 其思路大多是将ViewPager的getCount方法返回值设置为Integer.MAX_VALUE, 然后呢 ...
随机推荐
- SQL Server 2005 系统数据介绍:dm_exec_connections
原文:SQL Server 2005 系统数据介绍:dm_exec_connections 转载:http://msdn.microsoft.com/zh-cn/library/ms181509(SQ ...
- Delphi创建开机启动项的方法示例
Delphi可以通过创建开机启动项键值的方法,将程序添加到开机启动项中.通过本实例代码就可以为您的程序添加到快速启动中,随着Windows一起启动,开机即运行的程序.该实例代码简单,主要是通过添加注册 ...
- 3、列表 list
列表 >>> list=['aaa','bbb','ccc'] >>> print list ['aaa', 'bbb', 'ccc'] >>> ...
- QQ-weiyun(微云)-云储存
ylbtech-DatabaseDesgin:QQ-weiyun(微云)-云储存 1.A,数据库关系图(Database Diagram) -- =========================== ...
- 关于String的两种赋值方式
String的两种赋值是不同的,String str1=“hello”,指向堆内存中的"hello",而String str2=new String("hello&quo ...
- leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- tomcat修改默认访问首页
找到conf下server.xml文件修改如下位置内容 <Host name="localhost" appBase="webapps" unpackWA ...
- 【菜鸟学注入】之MySQL报错注入详解
本文转自:http://bbs.blackbap.org/forum.php?mod=viewthread&tid=6483&highlight=mysql%2B报错注入 用SQL注入 ...
- Yii2数据库分页操作方法介绍
本章节将介绍怎样怎样创建一个从数据表 country 中获取国家数据并显示出来的页面. 为了实现这个目标,你将会配置一个数据库连接.创建一个活动记录类,而且创建一个操作及一个视图. 贯穿整个章节,你将 ...
- MVVM模式源码分析手写实现
1.demo1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...