iOS ScrollView 无限循环

   2016-10-13 0
核心提示:用scrollview做,循环展示,1~10个可以比较简单,耗能比较少,假设100,1000,等等或者更多,总不能创建这么多个view。可以参考这个博客,写的漂亮,我是在这个基础上做了点封装这篇博客 主要介绍了 用3个view 来显示无限个 view,主要是通过- (void)scrollVi

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

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

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

我自定义了一个view EndlessLoopShowView

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

@end

@interface EndlessLoopShowView : UIView

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

@property (nonatomic,weak) id delegate;

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

#import "EndlessLoopShowView.h"

/*无限循环的视图 **/
@interface EndlessLoopShowView ()@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
frame="" nbsp="" 向左滑动="" self="" currentindex="" pragma="" mark="" -="" 公共方法="" void="" setimagedataarr:="" nsarray="" imagedataarr="" _imagedataarr="" imagecount="" 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);
}

文章转自 Mr_Lee_的简书

Demo链接: GitHub

 
标签: ScrollView iOS开发
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行