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, 然后呢 ...
随机推荐
- centos查找文件
centos查找文件 [root@localhost ~]# find / -name libmysqlclient.so
- 解读TDD的五大误区
所谓TDD简单地说就是以下两个步骤:确保所有的需求都能被照顾到:在代码不断增加和重构的过程中,可以检查所有的功能是否正确.本文我们一起来看下关于TDD的五大误区. TDD(全称Test Driven ...
- Windows环境下,用netstat命令查看某个端口号是否占用
目标:在Windows环境下,用netstat命令查看某个端口号是否占用,为哪个进程所占用. 操作:操作分为两步:(1)查看该端口被那个PID所占用;方法一:有针对性的查看端口,使用命令 Netsta ...
- pycharm批量清楚pyc、$py.class文件
By default, the .pyc and $py.class files are ignored, and thus are not visible in the Project tool w ...
- linux下小试redis demo
先启动 redis-server /etc/redis/redis.conf package com.test; import java.util.ArrayList; import java.ut ...
- C#实现在Form上截取消息的两种方法
比较常用的是重载Form的DefWndProc方法,例如截取鼠标按下的消息: protected override void DefWndProc(ref Message m) { if ( m.Ms ...
- 使用Spring的AbstractRoutingDataSource实现多数据源切换
https://www.cnblogs.com/softidea/p/7127874.html?utm_source=itdadao&utm_medium=referral https://b ...
- 微信小程序 - 一种简写方式
// 第一种 isOrder: function() { //code }, // 第二种 isOrders() { //code },
- springboot @Configuration
有了@Configuration,原来的springBean的配置文件可以去掉了, 原来在application.xml中配置的bean可以配置在@Configuration注解的来类中,使用@Bea ...
- Android监听HOME键的最简单的方法
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; public void onCreate(Bundle savedInsta ...