iOS-自定义UIScrollView实现无限滚动

学IOS感觉时间有点紧,都没有什么时间来深入了解一些基本控件,所以现在先封装一个ScrollView控件来给自己和大家用吧(希望大家可以理解,scrollView的本质是改变scrollView的bounds来实现的),希望可以给大家带来一些帮助,本控件实现可自动无限循环播放,使用3个imageView来显示,用户只需在实现一下初始化方法的时候

- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval : (NSTimeInterval)timeInterval;

将多个图片加入该自定义控件中,并设置自动播放的时间间隔,即可实现。

--------------------------------------------------------------------------

头文件liliLoopScrollView.h


#import<UIKit/UIKit.h>

@interface liliLoopScrollView : UIScrollView<UIScrollViewDelegate>

/** < 遵循协议 */@interface LoopScrollView : UIScrollView@property(nonatomic,strong) NSMutableArray * imageNameArr;/** < 播放的图片的名称数组 */

@property(nonatomic,strong) NSTimer * timer;/** < 计时器,用于实现自动播放 */

@property(nonatomic,assign) NSTimeInterval  timerInterval;/** < 用于设置自动播放的时间间隔 */

@property(nonatomic,strong) UIPageControl * pageController;/** < 分页 控件 */


/** < 初始化ScrollView 第一个参数为其位置及大小 第二个参数为要加入的图片的名字数组,第三个参数为自动滚动运行的间隔时间 */

- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval : (NSTimeInterval)timeInterval;

/** < 停止计时器,释放timer计时器 */

-(void)endTiming;

--------------------------------------------------------------------------

实现文件 liliLoopScrollView.m


#import "liliLoopScrollView.h"

@interface liliLoopScrollView ()

@property(nonatomic,strong) NSMutableArray * imageView;/** < 用于存放3个播放的图片视图 */

-(void)config;/** < 配置scrollView的基本设置 */

-(void)addImageViewToScrollView;/** < 将图片视图加入scrollView */

-(void)addImageToImageView;/** < 将图片加入imageView */

@end

@implementation liliLoopScrollView

- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr timeInterval:(NSTimeInterval)timeInterval

{

if (self = [super initWithFrame:frame]) {

self.timerInterval = timeInterval;

self.imageNameArr = arr;

 [self addImageViewToScrollView];

[self addImageToImageView];

[self config];

/** < 启动计时器 */

self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:self.timerInterval];

NSLog(@"self.timerInterval : %lf",self.timerInterval);

}

return self;

}

#pragma marks - config

- (void)config

{

self.delegate = self;/** < 设置代理 */

self.showsHorizontalScrollIndicator = NO;

self.showsVerticalScrollIndicator = NO;

self.bounces = NO;

self.userInteractionEnabled = YES;

self.contentSize = CGSizeMake(3 * self.bounds.size.width, self.bounds.size.height);

self.pagingEnabled = YES;

self.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);/** < 设置在中间视图 */

[self addSubview:self.pageController];

}

#pragma marks - addImageViewToScrollView

-(void)addImageViewToScrollView

{

for (int i = 0; i < 3; i ++) {

UIImageView * imageForView = [[UIImageView alloc] initWithFrame:CGRectMake(i * CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];

[self addSubview:imageForView];/** < 将视图加入scrollView中 */

[self.imageView addObject:imageForView];/** < 将视图加入用于播放的视图数组中 */

}

}

#pragma marks - addImageToimageView

- (void)addImageToImageView

{

int index = 0;

for (UIImageView * view in _imageView) {

view.image = [UIImage imageNamed:_imageNameArr[index]];

index ++;

}

}

#pragma marks - 实现协议方法,当滑动时,判断偏移量

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

/** < 左滑 */

if (self.contentOffset.x >= 2 * CGRectGetWidth(self.bounds)) {

NSString * firstImageName = [self.imageNameArr.firstObject mutableCopy];/** < 复制最后一张图片的名字 */

[self.imageNameArr removeObjectAtIndex:0];

[self.imageNameArr addObject:firstImageName];

/** < 配置分页控件 */

self.pageController.currentPage = self.pageController.currentPage == self.imageNameArr.count - 1 ? 0 : self.pageController.currentPage + 1;

/** < 右滑 */

}else if (self.contentOffset.x <= 0){

NSString * lastImageName = [self.imageNameArr.lastObject mutableCopy];

[self.imageNameArr removeLastObject];

[self.imageNameArr insertObject:lastImageName atIndex:0];

/** < 配置分页控件 */

self.pageController.currentPage = self.pageController.currentPage == 0 ? 7 : self.pageController.currentPage - 1;

}else{

return;

}

[self addImageToImageView];/** < 重新加载图片到图片视图中 */

self.contentOffset = CGPointMake(self.bounds.size.width, 0);/** < 还原偏移量至中间 */

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

/** < 开始拖动时,,暂停计时器 */

self.timer.fireDate = [NSDate distantFuture];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

/** < 停止拖动后,延迟timerInterval的时间开始计时 */

self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:_timerInterval];

}

/** < 停止计时器,释放timer计时器 */

-(void)endTiming

{

[self.timer invalidate];

}

#pragma marks - respondsToTimer

-(void)respondsToTimer

{

/** < 使用动画会造成一定的分页控件显示误差,就是慢一步 */

[UIView animateWithDuration:_timerInterval animations:^{

self.contentOffset = CGPointMake(2 * CGRectGetWidth(self.bounds), 0);

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

} completion:^(BOOL finished) {

}];

}

#pragma marks -  getter

- (NSMutableArray *)imageView

{

if (!_imageView) {

_imageView =[NSMutableArray array];

}

return _imageView;

}

- (NSMutableArray *)imageNameArr

{

if (!_imageNameArr) {

_imageNameArr =[NSMutableArray array];

}

return _imageNameArr;

}

- (UIPageControl *)pageController

{

if (!_pageController) {

_pageController = [[UIPageControl alloc] init];

_pageController.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 30);

_pageController.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.bounds) - CGRectGetMidY(_pageController.bounds));

_pageController.numberOfPages = self.imageNameArr.count;

_pageController.currentPage = 0;

_pageController.currentPageIndicatorTintColor = [UIColor redColor];

_pageController.pageIndicatorTintColor = [UIColor lightGrayColor];

}

return _pageController;

}

- (NSTimer *)timer

{

if (!_timer) {

_timer = [NSTimer scheduledTimerWithTimeInterval:_timerInterval target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];

}

return _timer;

}


这是我慢慢码的,希望您尊重我的劳动成果哟,要转载请注明出处哈:

http://www.jianshu.com/writer#/notebooks/1273630/notes/1658950

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

推荐阅读更多精彩内容