-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPZPhotoView.m
More file actions
394 lines (299 loc) · 14.1 KB
/
PZPhotoView.m
File metadata and controls
394 lines (299 loc) · 14.1 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// PZPhotoView.m
// PhotoZoom
//
// Created by Brennan Stehling on 10/27/12.
// Copyright (c) 2012 SmallSharptools LLC. All rights reserved.
//
#import "PZPhotoView.h"
#define kZoomStep 2
@interface PZPhotoView () <UIScrollViewDelegate>
@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UIActivityIndicatorView *activityIndicator;
@end
@implementation PZPhotoView {
CGPoint _pointToCenterAfterResize;
CGFloat _scaleToRestoreAfterResize;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setupView];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setupView];
}
- (void)setupView {
self.delegate = self;
self.imageView = nil;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = TRUE;
self.decelerationRate = UIScrollViewDecelerationRateFast;
UITapGestureRecognizer *scrollViewDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollViewDoubleTap:)];
[scrollViewDoubleTap setNumberOfTapsRequired:2];
[self addGestureRecognizer:scrollViewDoubleTap];
UITapGestureRecognizer *scrollViewTwoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollViewTwoFingerTap:)];
[scrollViewTwoFingerTap setNumberOfTouchesRequired:2];
[self addGestureRecognizer:scrollViewTwoFingerTap];
UITapGestureRecognizer *scrollViewSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollViewSingleTap:)];
[scrollViewSingleTap requireGestureRecognizerToFail:scrollViewDoubleTap];
[self addGestureRecognizer:scrollViewSingleTap];
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.imageView) {
// center the zoom view as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = self.imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
self.imageView.frame = frameToCenter;
CGPoint contentOffset = self.contentOffset;
// ensure horizontal offset is reasonable
if (frameToCenter.origin.x != 0.0)
contentOffset.x = 0.0;
// ensure vertical offset is reasonable
if (frameToCenter.origin.y != 0.0)
contentOffset.y = 0.0;
self.contentOffset = contentOffset;
// ensure content insert is zeroed out using translucent navigation bars
self.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}
}
- (void)setFrame:(CGRect)frame {
BOOL sizeChanging = !CGSizeEqualToSize(frame.size, self.frame.size);
if (sizeChanging) {
[self prepareToResize];
}
[super setFrame:frame];
if (sizeChanging) {
[self recoverFromResizing];
}
}
#pragma mark - Public Implementation
#pragma mark -
- (void)prepareForReuse {
// start by dropping any views and resetting the key properties
if (self.imageView != nil) {
for (UIGestureRecognizer *gestureRecognizer in self.imageView.gestureRecognizers) {
[self.imageView removeGestureRecognizer:gestureRecognizer];
}
}
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
self.imageView = nil;
self.activityIndicator = nil;
}
- (void)displayImage:(UIImage *)image {
if (self.imageView) {
[self.imageView removeFromSuperview];
self.imageView = nil;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = TRUE;
[self addSubview:imageView];
self.imageView = imageView;
// add gesture recognizers to the image view
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
UITapGestureRecognizer *doubleTwoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTwoFingerTap:)];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
[doubleTwoFingerTap setNumberOfTapsRequired:2];
[doubleTwoFingerTap setNumberOfTouchesRequired:2];
[singleTap requireGestureRecognizerToFail:doubleTap];
[twoFingerTap requireGestureRecognizerToFail:doubleTwoFingerTap];
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[self.imageView addGestureRecognizer:doubleTwoFingerTap];
self.contentSize = self.imageView.frame.size;
[self setMaxMinZoomScalesForCurrentBounds];
[self setZoomScale:self.minimumZoomScale animated:FALSE];
}
- (void)startWaiting {
if (!self.activityIndicator) {
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:activityIndicator];
activityIndicator.hidesWhenStopped = TRUE;
self.activityIndicator = activityIndicator;
}
self.activityIndicator.center = self.center;
[self bringSubviewToFront:self.activityIndicator];
[self.activityIndicator startAnimating];
self.imageView.hidden = TRUE;
}
- (void)stopWaiting {
[self.activityIndicator stopAnimating];
self.imageView.hidden = FALSE;
}
#pragma mark - Gestures
#pragma mark -
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.photoViewDelegate != nil && [self.photoViewDelegate respondsToSelector:@selector(photoViewDidSingleTap:)]) {
[self.photoViewDelegate photoViewDidSingleTap:self];
}
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.zoomScale > (self.maximumZoomScale - FLT_EPSILON)) {
// jump back to minimum scale
[self updateZoomScaleWithGesture:gestureRecognizer newScale:self.minimumZoomScale];
}
else {
// double tap zooms in
CGFloat newScale = MIN(self.zoomScale * kZoomStep, self.maximumZoomScale);
[self updateZoomScaleWithGesture:gestureRecognizer newScale:newScale];
}
if (self.photoViewDelegate != nil && [self.photoViewDelegate respondsToSelector:@selector(photoViewDidDoubleTap:)]) {
[self.photoViewDelegate photoViewDidDoubleTap:self];
}
}
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
// two-finger tap zooms out
CGFloat newScale = MAX([self zoomScale] / kZoomStep, self.minimumZoomScale);
[self updateZoomScaleWithGesture:gestureRecognizer newScale:newScale];
if (self.photoViewDelegate != nil && [self.photoViewDelegate respondsToSelector:@selector(photoViewDidTwoFingerTap:)]) {
[self.photoViewDelegate photoViewDidTwoFingerTap:self];
}
}
- (void)handleDoubleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.photoViewDelegate != nil && [self.photoViewDelegate respondsToSelector:@selector(photoViewDidDoubleTwoFingerTap:)]) {
[self.photoViewDelegate photoViewDidDoubleTwoFingerTap:self];
}
}
- (void)handleScrollViewSingleTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.photoViewDelegate != nil && [self.photoViewDelegate respondsToSelector:@selector(photoViewDidSingleTap:)]) {
[self.photoViewDelegate photoViewDidSingleTap:self];
}
}
- (void)handleScrollViewDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.imageView.image == nil) return;
CGPoint center =[self adjustPointIntoImageView:[gestureRecognizer locationInView:gestureRecognizer.view]];
if (!CGPointEqualToPoint(center, CGPointZero)) {
CGFloat newScale = MIN([self zoomScale] * kZoomStep, self.maximumZoomScale);
[self updateZoomScale:newScale withCenter:center];
}
}
- (void)handleScrollViewTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
if (self.imageView.image == nil) return;
CGPoint center =[self adjustPointIntoImageView:[gestureRecognizer locationInView:gestureRecognizer.view]];
if (!CGPointEqualToPoint(center, CGPointZero)) {
CGFloat newScale = MAX([self zoomScale] / kZoomStep, self.minimumZoomScale);
[self updateZoomScale:newScale withCenter:center];
}
}
- (CGPoint)adjustPointIntoImageView:(CGPoint)center {
BOOL contains = CGRectContainsPoint(self.imageView.frame, center);
if (!contains) {
center.x = center.x / self.zoomScale;
center.y = center.y / self.zoomScale;
// adjust center with bounds and scale to be a point within the image view bounds
CGRect imageViewBounds = self.imageView.bounds;
center.x = MAX(center.x, imageViewBounds.origin.x);
center.x = MIN(center.x, imageViewBounds.origin.x + imageViewBounds.size.height);
center.y = MAX(center.y, imageViewBounds.origin.y);
center.y = MIN(center.y, imageViewBounds.origin.y + imageViewBounds.size.width);
return center;
}
return CGPointZero;
}
#pragma mark - Support Methods
#pragma mark -
- (void)updateZoomScale:(CGFloat)newScale {
CGPoint center = CGPointMake(self.imageView.bounds.size.width/ 2.0, self.imageView.bounds.size.height / 2.0);
[self updateZoomScale:newScale withCenter:center];
}
- (void)updateZoomScaleWithGesture:(UIGestureRecognizer *)gestureRecognizer newScale:(CGFloat)newScale {
CGPoint center = [gestureRecognizer locationInView:gestureRecognizer.view];
[self updateZoomScale:newScale withCenter:center];
}
- (void)updateZoomScale:(CGFloat)newScale withCenter:(CGPoint)center {
newScale = MAX(self.minimumZoomScale, newScale);
newScale = MIN(self.maximumZoomScale, newScale);
if (self.zoomScale != newScale) {
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:center];
[self zoomToRect:zoomRect animated:YES];
}
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
scale = MAX(self.minimumZoomScale, scale);
scale = MIN(self.maximumZoomScale, scale);
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
zoomRect.size.width = self.frame.size.width / scale;
zoomRect.size.height = self.frame.size.height / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
- (void)setMaxMinZoomScalesForCurrentBounds {
// calculate minimum scale to perfectly fit image width, and begin at that scale
CGSize boundsSize = self.bounds.size;
CGFloat minScale = 0.25;
if (self.imageView.bounds.size.width > 0.0 && self.imageView.bounds.size.height > 0.0) {
// calculate min/max zoomscale
CGFloat xScale = boundsSize.width / self.imageView.bounds.size.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / self.imageView.bounds.size.height; // the scale needed to perfectly fit the image height-wise
minScale = MIN(xScale, yScale);
}
CGFloat maxScale = minScale * (kZoomStep * 2);
self.maximumZoomScale = maxScale;
self.minimumZoomScale = minScale;
}
- (void)prepareToResize {
CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
_pointToCenterAfterResize = [self convertPoint:boundsCenter toView:self.imageView];
_scaleToRestoreAfterResize = self.zoomScale;
// If we're at the minimum zoom scale, preserve that by returning 0, which will be converted to the minimum
// allowable scale when the scale is restored.
if (_scaleToRestoreAfterResize <= self.minimumZoomScale + FLT_EPSILON)
_scaleToRestoreAfterResize = 0;
}
- (void)recoverFromResizing {
[self setMaxMinZoomScalesForCurrentBounds];
// Step 1: restore zoom scale, first making sure it is within the allowable range.
CGFloat maxZoomScale = MAX(self.minimumZoomScale, _scaleToRestoreAfterResize);
self.zoomScale = MIN(self.maximumZoomScale, maxZoomScale);
// Step 2: restore center point, first making sure it is within the allowable range.
// 2a: convert our desired center point back to our own coordinate space
CGPoint boundsCenter = [self convertPoint:_pointToCenterAfterResize fromView:self.imageView];
// 2b: calculate the content offset that would yield that center point
CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0,
boundsCenter.y - self.bounds.size.height / 2.0);
// 2c: restore offset, adjusted to be within the allowable range
CGPoint maxOffset = [self maximumContentOffset];
CGPoint minOffset = [self minimumContentOffset];
CGFloat realMaxOffset = MIN(maxOffset.x, offset.x);
offset.x = MAX(minOffset.x, realMaxOffset);
realMaxOffset = MIN(maxOffset.y, offset.y);
offset.y = MAX(minOffset.y, realMaxOffset);
self.contentOffset = offset;
}
- (CGPoint)maximumContentOffset {
CGSize contentSize = self.contentSize;
CGSize boundsSize = self.bounds.size;
return CGPointMake(contentSize.width - boundsSize.width, contentSize.height - boundsSize.height);
}
- (CGPoint)minimumContentOffset {
return CGPointZero;
}
#pragma mark - UIScrollViewDelegate Methods
#pragma mark -
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
@end