forked from Janak-Nirmal/MFPageFlowView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFPageFlowView.m
205 lines (152 loc) · 5.35 KB
/
MFPageFlowView.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
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
//
// MFPageFlowView
// Mentally Friendly
//
// Created by Kyle Fuller on 24/09/2012.
// Copyright (c) 2012-2013 Mentally Friendly. All rights reserved.
//
#import "MFPageFlowView.h"
@interface MFPageFlowView ()
@property (nonatomic, assign) CGSize lastSize;
@property (nonatomic, strong) NSArray *pages;
@end
@implementation MFPageFlowView
- (id)init {
if (self = [super init]) {
[self setupDefaults];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupDefaults];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self setupDefaults];
}
return self;
}
- (void)setupDefaults {
_currentPage = 0;
[self setPagingEnabled:YES];
[self setShowsHorizontalScrollIndicator:NO];
[self setShowsVerticalScrollIndicator:NO];
[self setClipsToBounds:YES];
[self setScrollsToTop:NO];
}
- (void)reloadData {
[[self pages] makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSUInteger numberOfPages = [[self dataSource] numberOfPagesInFlowView:self];
NSMutableArray *pages = [[NSMutableArray alloc] initWithCapacity:numberOfPages];
for (NSUInteger index = 0; index < numberOfPages; index++) {
UIView *view = [[self dataSource] pageFlowView:self viewForPageAtIndex:index];
[view setClipsToBounds:YES];
[self addSubview:view];
[pages addObject:view];
}
[self setPages:pages];
[self updateContentSize];
[[self pageControl] setNumberOfPages:(NSInteger)numberOfPages];
}
#pragma mark - Setters
- (void)setCurrentPage:(NSUInteger)currentPage {
[self setCurrentPage:currentPage
animated:NO];
}
- (void)setCurrentPage:(NSUInteger)currentPage
animated:(BOOL)animated {
CGRect frame = [self frame];
CGFloat width = CGRectGetWidth(frame);
CGFloat x = (width * currentPage);
[self setContentOffset:CGPointMake(x, 0)
animated:animated];
}
- (void)setPageControl:(UIPageControl *)pageControl {
[[self pageControl] removeTarget:self
action:@selector(pageControlDidChange:)
forControlEvents:UIControlEventValueChanged];
_pageControl = pageControl;
[[self pageControl] addTarget:self
action:@selector(pageControlDidChange:)
forControlEvents:UIControlEventValueChanged];
NSUInteger numberOfPages = [[self dataSource] numberOfPagesInFlowView:self];
[[self pageControl] setNumberOfPages:(NSInteger)numberOfPages];
}
- (void)setDataSource:(id<MFPageFlowViewDataSource>)dataSource {
_dataSource = dataSource;
[self reloadData];
}
- (void)updateContentSize {
NSUInteger numberOfPages = [[self dataSource] numberOfPagesInFlowView:self];
CGRect frame = [self frame];
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
[self setContentSize:CGSizeMake(width * numberOfPages, height)];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = [self frame];
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
NSUInteger page = 0;
for (UIView *view in [self pages]) {
if ([self layoutOrientation] == MFPageFlowViewLayoutOrientationHorizontal) {
[view setFrame:CGRectMake(width * page, 0, width, height)];
} else {
[view setFrame:CGRectMake(0, width * page, width, height)];
}
++page;
}
[self updateContentSize];
// Make sure that we are on a page.
CGSize lastSize = [self lastSize];
if (CGSizeEqualToSize(lastSize, frame.size) == NO) {
[self setCurrentPage:_currentPage];
}
[self setLastSize:frame.size];
}
- (void)pageControlDidChange:(UIPageControl*)pageControl {
NSInteger page = [pageControl currentPage];
CGRect frame = [self frame];
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
CGRect scrollFrame;
switch ([self layoutOrientation]) {
case MFPageFlowViewLayoutOrientationHorizontal: {
scrollFrame = CGRectMake(width * page, 0, width, height);
break;
}
case MFPageFlowViewLayoutOrientationVertical: {
scrollFrame = CGRectMake(0, width * page, width, height);
break;
}
}
[self scrollRectToVisible:scrollFrame animated:YES];
}
- (void)setContentOffset:(CGPoint)contentOffset {
[super setContentOffset:contentOffset];
CGRect frame = [self frame];
NSUInteger currentPage;
switch ([self layoutOrientation]) {
case MFPageFlowViewLayoutOrientationHorizontal: {
CGFloat pageWidth = CGRectGetWidth(frame);
currentPage = lroundf(floorf((contentOffset.x - (pageWidth / 2.0f)) / pageWidth) + 1.0f);
break;
}
case MFPageFlowViewLayoutOrientationVertical: {
CGFloat pageHeight = CGRectGetHeight(frame);
currentPage = lroundf(floorf((contentOffset.y - (pageHeight / 2.0f)) / pageHeight) + 1.0f);
break;
}
}
if (_currentPage != currentPage) {
[[self pageControl] setCurrentPage:currentPage];
if (currentPage <= [[self dataSource] numberOfPagesInFlowView:self]) {
_currentPage = currentPage;
}
}
}
@end