-
Notifications
You must be signed in to change notification settings - Fork 36
/
ARTiledImageScrollView.m
252 lines (185 loc) · 7.23 KB
/
ARTiledImageScrollView.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
//
// ARTiledImageViewScrollView.m
// ARTiledImageView
//
// Created by Daniel Doubrovkine on 3/10/14.
// Copyright (c) 2014 Artsy. All rights reserved.
//
#import "ARTiledImageScrollView.h"
#import "ARTiledImageView.h"
#import "ARImageBackedTiledView.h"
#import "ARTiledImageViewDataSource.h"
#import <SDWebImage/UIImageView+WebCache.h>
const CGFloat ARTiledImageScrollViewDefaultZoomStep = 1.5;
@interface ARTiledImageScrollView ()
@property (nonatomic, weak, readonly) ARImageBackedTiledView *imageBackedTiledImageView;
@end
@implementation ARTiledImageScrollView
- (void)setDataSource:(NSObject <ARTiledImageViewDataSource> *)dataSource
{
_dataSource = dataSource;
[self setup];
}
- (void)setup
{
ARTiledImageView *tiledImageView = [[ARTiledImageView alloc] initWithDataSource:self.dataSource];
ARImageBackedTiledView *imageBackedTileView = [[ARImageBackedTiledView alloc] initWithTiledImageView:tiledImageView];
[self addSubview:imageBackedTileView];
_imageBackedTiledImageView = imageBackedTileView;
[self setMaxMinZoomScalesForCurrentBounds];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.delegate = self;
self.centerOnZoomOut = YES;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self addGestureRecognizer:doubleTap];
_doubleTapGesture = doubleTap;
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
[twoFingerTap setNumberOfTouchesRequired:2];
[self addGestureRecognizer:twoFingerTap];
[self.panGestureRecognizer addTarget:self action:@selector(mapPanGestureHandler:)];
}
- (void)setDisplayTileBorders:(BOOL)displayTileBorders
{
self.tiledImageView.displayTileBorders = displayTileBorders;
_displayTileBorders = displayTileBorders;
}
- (void)setMaxMinZoomScalesForCurrentBounds
{
CGSize boundsSize = self.bounds.size;
CGSize imageSize = [self.dataSource imageSizeForImageView:nil];
// Calculate min/max zoomscale.
CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
CGFloat minScale = 0;
if (self.contentMode == UIViewContentModeScaleAspectFit) {
minScale = MIN(xScale, yScale);
} else {
minScale = MAX(xScale, yScale);
}
CGFloat maxScale = 1.0;
// Don't let minScale exceed maxScale.
// If the image is smaller than the screen, we don't want to force it to be zoomed.
if (minScale > maxScale) {
minScale = maxScale;
}
self.maximumZoomScale = maxScale * 0.6;
self.minimumZoomScale = minScale;
self.originalSize = imageSize;
self.contentSize = boundsSize;
}
- (void)zoomToFit:(BOOL)animate
{
[self setZoomScale:self.minimumZoomScale animated:animate];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if (self.tileZoomLevel != self.tiledImageView.currentZoomLevel) {
_tileZoomLevel = self.tiledImageView.currentZoomLevel;
[self tileZoomLevelDidChange];
}
[self centerContent];
}
- (void)tileZoomLevelDidChange
{
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
BOOL zoomedOut = self.zoomScale == self.minimumZoomScale;
if (!CGPointEqualToPoint(self.centerPoint, CGPointZero) && !zoomedOut) {
[self centerOnPoint:self.centerPoint animated:NO];
}
[self centerContent];
}
- (void)setBackgroundImageURL:(NSURL *)backgroundImageURL
{
[self.backgroundImageView setImageWithURL:backgroundImageURL];
_backgroundImageURL = backgroundImageURL;
}
- (void)setBackgroundImage:(UIImage *)backgroundImage
{
[self.backgroundImageView setImage:backgroundImage];
_backgroundImage = backgroundImage;
}
- (void)mapPanGestureHandler:(UIPanGestureRecognizer *)panGesture
{
if (panGesture.state == UIGestureRecognizerStateBegan) {
_centerPoint = CGPointZero;
}
}
- (void)centerOnPoint:(CGPoint)point animated:(BOOL)animate
{
CGFloat x = (point.x * self.zoomScale) - (self.frame.size.width / 2.0f);
CGFloat y = (point.y * self.zoomScale) - (self.frame.size.height / 2.0f);
[self setContentOffset:CGPointMake(round(x), round(y)) animated:animate];
_centerPoint = point;
}
- (CGPoint)zoomRelativePoint:(CGPoint)point
{
CGFloat x = (self.contentSize.width / self.originalSize.width) * point.x;
CGFloat y = (self.contentSize.height / self.originalSize.height) * point.y;
return CGPointMake(round(x), round(y));
}
#pragma mark - Orientation
- (void)centerContent {
if (!self.centerOnZoomOut) { return; }
CGFloat top = 0, left = 0;
if (self.contentSize.width < self.bounds.size.width) {
left = (self.bounds.size.width-self.contentSize.width) * 0.5f;
}
if (self.contentSize.height < self.bounds.size.height) {
top = (self.bounds.size.height-self.contentSize.height) * 0.5f;
}
self.contentInset = UIEdgeInsetsMake(top, left, top, left);
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageBackedTiledImageView;
}
- (CGFloat)zoomLevel
{
return self.zoomScale / self.maximumZoomScale;
}
#pragma mark - Tap to Zoom
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{
// Double tap zooms in, but returns to normal zoom level if it reaches max zoom.
if (self.zoomScale >= self.maximumZoomScale) {
[self setZoomScale:self.minimumZoomScale animated:YES];
} else {
// The location tapped becomes the new center
CGPoint tapCenter = [gestureRecognizer locationInView:self.tiledImageView];
CGFloat newScale = MIN(self.zoomScale * (self.zoomStep ? : ARTiledImageScrollViewDefaultZoomStep), self.maximumZoomScale);
CGRect maxZoomRect = [self rectAroundPoint:tapCenter atZoomScale:newScale];
[self zoomToRect:maxZoomRect animated:YES];
}
}
- (CGRect)rectAroundPoint:(CGPoint)point atZoomScale:(CGFloat)zoomScale
{
// Define the shape of the zoom rect.
CGSize boundsSize = self.bounds.size;
// Modify the size according to the requested zoom level.
// For example, if we're zooming in to 0.5 zoom, then this will increase the bounds size by a factor of two.
CGSize scaledBoundsSize = CGSizeMake(boundsSize.width / zoomScale, boundsSize.height / zoomScale);
return CGRectMake(point.x - scaledBoundsSize.width / 2,
point.y - scaledBoundsSize.height / 2,
scaledBoundsSize.width,
scaledBoundsSize.height);
}
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer
{
// Two-finger tap zooms out, but returns to normal zoom level if it reaches min zoom.
CGFloat newScale = self.zoomScale <= self.minimumZoomScale ? self.maximumZoomScale : self.zoomScale / (self.zoomStep ? : ARTiledImageScrollViewDefaultZoomStep);
[self setZoomScale:newScale animated:YES];
}
- (ARTiledImageView *)tiledImageView
{
return self.imageBackedTiledImageView.tiledImageView;
}
- (UIImageView *)backgroundImageView
{
return self.imageBackedTiledImageView.imageView;
}
@end