forked from vfr/Reader
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPDFViewController.m
260 lines (212 loc) · 8.47 KB
/
PDFViewController.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// PDFViewController.m
// Reader
//
// Created by Jade Meskill on 11/15/10.
// Copyright 2010 example.com. All rights reserved.
//
#import "PDFViewController.h"
#import "PDFViewTiled.h"
#import "PDFScrollView.h"
#import "PDFContainer.h"
@implementation PDFViewController
@synthesize pagingScrollView;
#define ZOOM_AMOUNT 0.25f
#define NO_ZOOM_SCALE 1.0f
#define MINIMUM_ZOOM_SCALE 1.0f
#define MAXIMUM_ZOOM_SCALE 5.0f
#define NAV_AREA_SIZE 48.0f
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)initPDFScroll {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
pagingScrollView.delegate = self;
[self.view addSubview:self.pagingScrollView];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}
- (PDFScrollView *)dequeueRecycledPage
{
PDFScrollView *page = [recycledPages anyObject];
if (page) {
[[page retain] autorelease];
[recycledPages removeObject:page];
}
return page;
}
- (BOOL)isDisplayingPageForIndex:(NSUInteger)index
{
BOOL foundPage = NO;
for (PDFScrollView *page in visiblePages) {
if (page.index - 1 == index) {
foundPage = YES;
break;
}
}
return foundPage;
}
- (PDFScrollView *)currentlyDisplayedPage {
for (PDFScrollView *page in visiblePages) {
return page;
break;
}
return nil;
}
#pragma mark -
#pragma mark ScrollView delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self tilePages];
}
#pragma mark -
#pragma mark View controller rotation methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// here, our pagingScrollView bounds have not yet been updated for the new interface orientation. So this is a good
// place to calculate the content offset that we will need in the new orientation
// CGFloat offset = pagingScrollView.contentOffset.x;
// CGFloat pageWidth = pagingScrollView.bounds.size.width;
//
// if (offset >= 0) {
// firstVisiblePageIndexBeforeRotation = floorf(offset / pageWidth);
// percentScrolledIntoFirstVisiblePage = (offset - (firstVisiblePageIndexBeforeRotation * pageWidth)) / pageWidth;
// } else {
// firstVisiblePageIndexBeforeRotation = 0;
// percentScrolledIntoFirstVisiblePage = offset / pageWidth;
// }
self.pagingScrollView.zoomScale = NO_ZOOM_SCALE;
[[self currentlyDisplayedPage] willRotate];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// recalculate contentSize based on current orientation
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
// // adjust frames and configuration of each visible page
for (PDFScrollView *page in visiblePages) {
// CGPoint restorePoint = [page pointToCenterAfterRotation];
// CGFloat restoreScale = [page scaleToRestoreAfterRotation];
page.frame = [self frameForPageAtIndex:page.index - 1];
// page.zoomScale = 1.0;
// [page restoreCenterPoint:restorePoint scale:restoreScale];
}
for (PDFScrollView *page in recycledPages) {
page.frame = [self frameForPageAtIndex:page.index - 1];
}
//
// // adjust contentOffset to preserve page location based on values collected prior to location
// CGFloat pageWidth = pagingScrollView.bounds.size.width;
// CGFloat newOffset = (firstVisiblePageIndexBeforeRotation * pageWidth) + (percentScrolledIntoFirstVisiblePage * pageWidth);
// pagingScrollView.contentOffset = CGPointMake(newOffset, 0);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
#ifdef DEBUG
NSLog(@"ReaderViewController.m -didRotateFromInterfaceOrientation: [%d] to [%d]", fromInterfaceOrientation, self.interfaceOrientation);
NSLog(@" -> self.view.bounds = %@", NSStringFromCGRect(self.view.bounds));
#endif
// recalculate contentSize based on current orientation
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
[[self currentlyDisplayedPage] didRotate];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[pagingScrollView release]; pagingScrollView = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Tiling and page configuration
- (void)tilePages
{
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, [self pageCount] - 1);
// Recycle no-longer-visible pages
for (PDFScrollView *page in visiblePages) {
if (page.index - 1 < firstNeededPageIndex || page.index - 1 > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
PDFScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[PDFScrollView alloc] initWithPage:index + 1 frame:[self frameForPageAtIndex:index]] autorelease];
}
page.zoomScale = 1.0;
[pagingScrollView addSubview:page];
//[self setMaxMinZoomScalesForCurrentBounds];
[visiblePages addObject:page];
}
}
}
#pragma mark -
#pragma mark Frame calculations
#define PADDING 10
- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
// We have to use our paging scroll view's bounds, not frame, to calculate the page placement. When the device is in
// landscape orientation, the frame will still be in portrait because the pagingScrollView is the root view controller's
// view, so its frame is in window coordinate space, which is never rotated. Its bounds, however, will be in landscape
// because it has a rotation transform applied.
CGRect bounds = pagingScrollView.bounds;
CGRect pageFrame = bounds;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (bounds.size.width * index) + PADDING;
return pageFrame;
}
- (CGSize)contentSizeForPagingScrollView {
// We have to use the paging scroll view's bounds to calculate the contentSize, for the same reason outlined above.
CGRect bounds = pagingScrollView.bounds;
return CGSizeMake(bounds.size.width * [self pageCount], bounds.size.height);
}
- (NSInteger)pageCount {
return [[PDFContainer sharedPDF] pages];
}
@end