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, 然后呢 ...
随机推荐
- SqlServer发布订阅错误收集
原文:SqlServer发布订阅错误收集 目录 1. SqlServer发布订阅错误收集 1.1. Message:脚本对于表"dbo.table"失败. 1.1.1. 错误消息 ...
- openfire源码研究笔记:对设计模式及原则的学习
原文:http://blog.csdn.net/jinzhencs/article/details/50522105 一.拦截器的实现 地点: package org.jivesoftware.o ...
- 咏南下拉列表非数据敏感控件--TYNSearch
咏南下拉列表非数据敏感控件--TYNSearch 拥有下拉列表控件可以大大地加速软件系统的开发. 控件适用于DELPHI5及以上版本的安装和使用. 控件的使用方法: procedure Tflog.s ...
- [置顶]
kubernetes资源对象--Horizontal Pod Autoscaling(HPA)
概念 HPA全称Horizontal Pod Autoscaling,即pod的水平自动扩展.自动扩展主要分为两种,其一为水平扩展,针对于实例数目的增减:其二为垂直扩展,即单个实例可以使用的资源的增减 ...
- 上机题目(0基础)- Java网络操作-Socket实现client和server端通信二(Java)
上一节实现了client像server端发送请求.本节将实现server端向client回传信息.实现原理非常easy,在原来的基础上.在server端实现输出流,在client实现输入流就可以,详细 ...
- python pep8编码风格学习记录(转)
简要归纳 每个缩进层级使用4个空格. 每行最多79个字符. 顶层的函数或类的定义之间空两行. 采用ASCII或UTF-8编码文件 在文件顶端,注释和文档说明之下,每行每条import语句只导入一个模块 ...
- 新人补钙系列教程之:3D理论 - 二进制空间分割(BSP)树
1. 什么是BSP树 BSP算法的初始数据是一个多边形集,BSP在预处理的时候先在多边形集中选取一个多边形作为支持平面,然后根据这个平面将集合划分成两个部分,每个部分是一个新的子节点,递归进行该过程, ...
- HDU4674 Trip Advisor
Problem Description There is a strange country somewhere which its transportation network was built ...
- C# Socket.Connect连接请求超时机制
介绍 您可能注意到了,.Net的System.Net.Sockets.TcpClient和System.Net.Sockets.Socket都没有直接为Connect/BeginConnect提供超时 ...
- react-native 初始化 各种报错 及 解决方案
1.Unable to load script from assets 'index.android.bundle'. curl -k "http://localhost:8081/inde ...