-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWebPImagePageView.m
132 lines (114 loc) · 3.7 KB
/
WebPImagePageView.m
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
//
// WebPImagePageView.m
// ImageButter
//
// Created by Dalton Cherry on 12/7/15.
//
#import "WebPImagePageView.h"
#import "WebPImageView.h"
@interface Recycle : NSObject
@property(nonatomic)WebPImageView *view;
@property(nonatomic)NSInteger index;
@end
@interface WebPImagePageView () <UIScrollViewDelegate>
@property(nonatomic)UIScrollView *scrollView;
@property(nonatomic)NSMutableSet *recycleSet;
@property(nonatomic)NSMutableSet *currentSet;
@property(nonatomic)NSInteger currentIndex;
@end
@implementation WebPImagePageView
-(instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self commonInit];
}
return self;
}
- (void)commonInit {
self.backgroundColor = [UIColor blackColor];
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:self.scrollView];
self.recycleSet = [[NSMutableSet alloc] init];
self.currentSet = [[NSMutableSet alloc] init];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat spacing = 10;
self.scrollView.frame = CGRectMake(0, 0, self.bounds.size.width+spacing, self.bounds.size.height);
self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width*self.urls.count,
self.scrollView.bounds.size.height);
[self showPage];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSInteger index = scrollView.contentOffset.x/self.scrollView.frame.size.width;
if(index < 0) {
index = 0;
} else if(index > self.urls.count-1) {
index = self.urls.count-1;
}
BOOL didChange = NO;
if(index != self.currentIndex) {
didChange = YES;
}
self.currentIndex = index;
if(didChange) {
[self.delegate didChangeImage:self index:self.currentIndex];
[self showPage];
}
}
- (void)showPage {
if(self.scrollView.bounds.size.width == 0) {
return;
}
//remove old views from screen and add to recycle
NSInteger indexOffset = 1;
for(Recycle *item in [self.currentSet allObjects]) {
if(item.index < self.currentIndex-indexOffset || item.index > self.currentIndex+indexOffset) {
item.view.pause = YES;
item.view.image = nil;
[item.view removeFromSuperview];
[self.currentSet removeObject:item];
[self.recycleSet addObject:item];
}
}
//add the views
for(NSInteger i = self.currentIndex-indexOffset; i <= self.currentIndex+indexOffset; i++) {
if(i > -1 && i < self.urls.count) {
Recycle *item = [self dequeueItem];
item.index = i;
item.view.url = self.urls[i];
if(i == self.currentIndex) {
item.view.pause = NO;
} else {
item.view.pause = YES;
}
CGFloat x = self.scrollView.frame.size.width*i;
item.view.frame = CGRectMake(x, 0, self.bounds.size.width, self.bounds.size.height);
[self.scrollView addSubview:item.view];
[self.currentSet addObject:item];
}
}
}
- (Recycle*)dequeueItem {
Recycle *item = [self.recycleSet anyObject];
if(!item) {
item = [[Recycle alloc] init];
item.view = [[WebPImageView alloc] init];
} else {
[self.recycleSet removeObject:item];
}
return item;
}
- (void)setUrls:(NSArray *)urls {
_urls = urls;
[self setNeedsLayout];
self.currentIndex = 0;
self.scrollView.contentOffset = CGPointMake(0, 0);
[self showPage];
}
@end
@implementation Recycle
@end