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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// GMInfinitelView.m
//
//
// Created by wangyang on 10/12/15.
// Copyright (c) 2015年 Wanmeichuangyi. All rights reserved.
//
#import "GMInfiniteScrollView.h"
#import "GMImageView.h"
#define IMAGE_TAG 1000
@implementation GMInfiniteScrollView
- (void)setup{
[super setup];
// 显示的是图片数组里的第一张图片
// 和数组是+1关系
_currentPage = 0;
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.backgroundColor = UIColor.whiteColor;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.scrollsToTop = NO;
[self addSubview:_scrollView];
self.pageControl = [UIPageControl new];
self.pageControl.currentPage = 0;
self.pageControl.hidesForSinglePage = YES;
[self addSubview:self.pageControl];
self.imageContentMode = UIViewContentModeScaleAspectFill;
// 在应用后台时关闭播放,前台时开启
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopRolling) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startRolling) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)layoutSubviews{
[super layoutSubviews];
self.pageControl.frame = CGRectMake(0, self.frame.size.height-15, self.frame.size.width, 15);
self.pageControl.center = CGPointMake(self.frame.size.width / 2, self.pageControl.center.y);
}
- (void)dealloc
{
self.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rollingScrollAction) object:nil];
}
#pragma mark - Setter
- (void)setImageArray:(NSArray *)imageArray{
_imageArray = imageArray;
if(_imageArray.count == 0) {
return;
}
// 如果只有一个,不允许滑动
_scrollView.scrollEnabled = _imageArray.count == 1 ? NO : YES;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * 3,
_scrollView.frame.size.height);
for (NSInteger i = 0; i < 3; i++){
CGRect frame = CGRectOffset(_scrollView.bounds, _scrollView.frame.size.width * i, 0);
GMImageView *imageView = [[GMImageView alloc] initWithFrame:frame];
imageView.userInteractionEnabled = YES;
imageView.clipsToBounds = YES;
imageView.contentMode = self.imageContentMode;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imageView.tag = i + IMAGE_TAG;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[imageView addGestureRecognizer:singleTap];
[_scrollView addSubview:imageView];
}
self.pageControl.numberOfPages = self.self.imageArray.count;
_currentPage = 1;
[self refreshScrollView];
}
#pragma mark - Custom Method
- (void)refreshScrollView{
NSArray *imageArray = [self getDisplayImagesWithPageIndex:_currentPage];
for (NSInteger i = 0; i < 3; i++){
GMImageView *imageView = (GMImageView *)[_scrollView viewWithTag:i + IMAGE_TAG];
NSString *url = imageArray[i];
[imageView setImageWithUrlString:url placeHolder:nil];
}
// 水平滚动
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
self.pageControl.currentPage = _currentPage-1;
}
- (NSArray *)getDisplayImagesWithPageIndex:(NSInteger)page{
NSInteger pre = [self getPageIndex:_currentPage-1];
NSInteger last = [self getPageIndex:_currentPage+1];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:0];
[images addObject:[self.imageArray objectAtIndex:pre-1]];
[images addObject:[self.imageArray objectAtIndex:_currentPage-1]];
[images addObject:[self.imageArray objectAtIndex:last-1]];
return images;
}
- (NSInteger)getPageIndex:(NSInteger)index{
// value=1为第一张,value = 0为前面一张
if (index == 0)
{
index = self.imageArray.count;
}
if (index == self.imageArray.count + 1)
{
index = 1;
}
return index;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSInteger x = scrollView.contentOffset.x;
//取消已加入的延迟线程
if (self.enableRolling)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rollingScrollAction) object:nil];
}
// 往下翻一张
if (x >= 2 * _scrollView.frame.size.width)
{
_currentPage = [self getPageIndex:_currentPage+1];
[self refreshScrollView];
}
if (x <= 0)
{
_currentPage = [self getPageIndex:_currentPage-1];
[self refreshScrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
if (self.enableRolling)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rollingScrollAction) object:nil];
[self performSelector:@selector(rollingScrollAction) withObject:nil afterDelay:self.timeInterval inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
}
#pragma mark - Action
- (void)startRolling{
if (self.imageArray.count == 0 || self.imageArray.count == 1){
return;
}
[self stopRolling];
self.enableRolling = YES;
[self performSelector:@selector(rollingScrollAction) withObject:nil afterDelay:self.timeInterval];
}
- (void)stopRolling{
self.enableRolling = NO;
//取消已加入的延迟线程
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rollingScrollAction) object:nil];
}
- (void)rollingScrollAction{
// 滑动动画由我们自己完成
[UIView animateWithDuration:0.25 animations:^{
_scrollView.contentOffset = CGPointMake(1.99*_scrollView.frame.size.width, 0);
} completion:^(BOOL finished) {
if (finished)
{
_currentPage = [self getPageIndex:_currentPage+1];
[self refreshScrollView];
if (self.enableRolling)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rollingScrollAction) object:nil];
[self performSelector:@selector(rollingScrollAction) withObject:nil afterDelay:self.timeInterval inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
}
}];
}
- (void)handleTap:(UITapGestureRecognizer *)tap{
if ([self.delegate respondsToSelector:@selector(infiniteScrollView:didSelectIndex:)]){
[self.delegate infiniteScrollView:self didSelectIndex:_currentPage-1];
}
}
@end