iOS scrollView 无限循环

scrollview 做,循环展示,1~10个可以比较简单,耗能比较少,假设100,1000,等等或者更多,总不能创建这么多个view。

可以参考这个博客,写的漂亮,我是在这个基础上做了点封装

这篇博客 主要介绍了 用3个view 来显示无限个 view,主要是通过- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 来计算当前滑动了到第几个,然后调整 3个view的位置。

我自定义了一个view EndlessLoopShowView

#import <UIKit/UIKit.h>

@class EndlessLoopShowView;
@protocol EndlessLoopShowViewDelegate <NSObject>

@optional
/* 滑动到第几个位置 **/
- (void)endlessLoop:(EndlessLoopShowView*)showView scrollToIndex:(NSInteger)currentIndex;

@end

@interface EndlessLoopShowView : UIView

/* 返回图片的显示,可以传入 image ,imageName ,url ,自己可以自定义 **/
@property (nonatomic,strong) NSArray * imageDataArr;

@property (nonatomic,weak) id <EndlessLoopShowViewDelegate>delegate;

@end
//
//  EndlessLoopShowView.m
//  EndlessLoopShowView
//
//  Created by apple on 16/9/26.
//  Copyright © 2016年 李重阳. All rights reserved.
//

#import "EndlessLoopShowView.h"

/*无限循环的视图 **/
@interface EndlessLoopShowView ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,strong) UIImageView *leftImageView;
@property (nonatomic,strong) UIImageView *centerImageView;
@property (nonatomic,strong) UIImageView *rightImageView;

@property (nonatomic,assign) NSInteger currentIndex;/* 当前滑动到了哪个位置**/
@property (nonatomic,assign) NSInteger imageCount;/* 图片的总个数 **/


//http://www.cnblogs.com/kenshincui/p/3913885.html#ImageViewer
@end

@implementation EndlessLoopShowView

#pragma mark - 生命周期

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    _currentIndex = -1;
    [self addSubview:self.scrollView];
    //添加图片控件
    [self addImageViews];
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    CGFloat imageW = CGRectGetWidth(self.scrollView.bounds);
    CGFloat imageH = CGRectGetHeight(self.scrollView.bounds);
    self.leftImageView.frame   = CGRectMake(imageW*0, 0, imageW, imageH);
    self.centerImageView.frame = CGRectMake(imageW*1, 0, imageW, imageH);
    self.rightImageView.frame  = CGRectMake(imageW*2, 0, imageW, imageH);
    self.scrollView.contentSize= CGSizeMake(imageW*3, 0);
    self.currentIndex = 0;
    [self setScrollViewContentOffsetCenter];
}


#pragma mark - 私有方法

#pragma mark - get/set方法
- (UIScrollView *)scrollView {
    
    if (_scrollView == nil) {
        
        _scrollView=[[UIScrollView alloc]init];
        //设置代理
        _scrollView.delegate=self;
        //设置分页
        _scrollView.pagingEnabled=YES;
        //去掉滚动条
        _scrollView.showsHorizontalScrollIndicator=NO;
    }
    return _scrollView;
}

/* 重写 setCurrent 方法 并且赋值 **/
- (void)setCurrentIndex:(NSInteger)currentIndex {
    
    if (_currentIndex != currentIndex) {
        
        _currentIndex = currentIndex;
        
        NSInteger leftImageIndex = (currentIndex+_imageCount-1)%_imageCount;
        NSInteger rightImageIndex= (currentIndex+1)%_imageCount;
        _centerImageView.image =[UIImage imageNamed:self.imageDataArr[currentIndex]];
        _leftImageView.image   =[UIImage imageNamed:self.imageDataArr[leftImageIndex]];
        _rightImageView.image  =[UIImage imageNamed:self.imageDataArr[rightImageIndex]];
        
        [self setScrollViewContentOffsetCenter];
        
        if ([self.delegate respondsToSelector:@selector(endlessLoop:scrollToIndex:)]) {
            
            [self.delegate endlessLoop:self scrollToIndex:currentIndex];
        }
        
    }
}

#pragma mark 添加图片三个控件
-(void)addImageViews {
    
    _leftImageView=[[UIImageView alloc]init];
    _leftImageView.contentMode=UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_leftImageView];
    
    _centerImageView=[[UIImageView alloc]init];
    _centerImageView.contentMode=UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_centerImageView];
    
    _rightImageView=[[UIImageView alloc]init];
    _rightImageView.contentMode=UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_rightImageView];
}


/* 把scrollView 偏移到中心位置 **/
- (void)setScrollViewContentOffsetCenter {
    
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.bounds), 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGPoint offset=[_scrollView contentOffset];
    if (offset.x>CGRectGetWidth(scrollView.frame)) { //向右滑动
        self.currentIndex=(self.currentIndex+1)%_imageCount;
    }else if(offset.x<CGRectGetWidth(scrollView.frame)){ //向左滑动
        self.currentIndex=(self.currentIndex+_imageCount-1)%_imageCount;
    }
}


#pragma mark - 公共方法
- (void)setImageDataArr:(NSArray *)imageDataArr {
    
    _imageDataArr = imageDataArr;
    self.imageCount = imageDataArr.count;
    self.currentIndex = 0;
}

@end

用法

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    EndlessLoopShowView * showView = [[EndlessLoopShowView alloc]initWithFrame:CGRectMake(0, 100, 200, 200)];
    showView.backgroundColor = [UIColor redColor];
    showView.imageDataArr = @[@"1",@"2",@"3",@"4",@"5"];
    showView.delegate = self;
    [self.view addSubview:showView];
    
}

- (void)endlessLoop:(EndlessLoopShowView *)showView scrollToIndex:(NSInteger)currentIndex {
    
    NSLog(@"currentIndex = %ld",currentIndex);
}

连接
gitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,050评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,538评论 1 306
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,673评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,622评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,047评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,974评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,129评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,893评论 0 209
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,654评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,828评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,297评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,619评论 3 262
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,326评论 3 243
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,176评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,975评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,118评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,909评论 2 278

推荐阅读更多精彩内容