-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageScrollView.m
executable file
·228 lines (174 loc) · 8.28 KB
/
ImageScrollView.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
/*
File: ImageScrollView.m
Abstract: Centers image within the scroll view and configures image sizing and display.
Based on ImageScrollView Version: 1.1 by Apple
Copyright (c) 2012 Museum Victoria
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "ImageScrollView.h"
@implementation ImageScrollView
@synthesize index;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
//NSString *path = [[NSBundle mainBundle] pathForResource:@"defaultImageview" ofType:@"jpg"];
//[self displayImage:[UIImage imageWithContentsOfFile:path]];
}
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];
/*UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[singleFingerTap requireGestureRecognizerToFail:singleFingerDTap];
[self addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
*/
return self;
}
- (void)dealloc
{
self.delegate = nil;
[imageView release];
[super dealloc];
}
#pragma mark -
#pragma mark TapHandlers
-(void) handleSingleDoubleTap:(UIGestureRecognizer *)sender{
NSLog(@"DoubleTapHandled");
if (self.zoomScale == self.maximumZoomScale) {
[self setZoomScale:self.minimumZoomScale animated:YES];
}else {
[self setZoomScale:self.maximumZoomScale animated:YES];
}
}
-(void) handleSingleTap:(UIGestureRecognizer *)sender{
//Pass to Parent - Needs to bubble up to top level
NSLog(@"ImageScrollView - HandleSingleTap");
// if ([self.delegate respondsToSelector:@selector(handleSingleTap:)]) {
// [self.delegate handleSingleTap:sender];
// }
}
#pragma mark Override layoutSubviews to center content
- (void)layoutSubviews
{
[super layoutSubviews];
NSLog(@"ImageScrollView:LayoutSubviews");
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = 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;
imageView.frame = frameToCenter;
/* if ([imageView isKindOfClass:[TilingView class]]) {
// to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the
// tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens,
// which would cause the CATiledLayer to ask us for tiles of the wrong scales.)
imageView.contentScaleFactor = 1.0;
}*/
}
#pragma mark -
#pragma mark UIScrollView delegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
#pragma mark -
#pragma mark Configure scrollView to display new image (tiled or not)
- (void)displayImage:(UIImage *)image
{
// clear the previous imageView
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
// reset our zoomScale to 1.0 before doing any further calculations
self.zoomScale = 1.0;
// make a new UIImageView for the new image
imageView = [[UIImageView alloc] initWithImage:image];
[self addSubview:imageView];
self.contentSize = [image size];
[self setMaxMinZoomScalesForCurrentBounds];
self.zoomScale = self.minimumZoomScale;
}
- (void)setMaxMinZoomScalesForCurrentBounds
{
CGSize boundsSize = self.bounds.size;
CGSize imageSize = imageView.bounds.size;
// 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 = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
// on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
// maximum zoom scale to 0.5.
CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];
// 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;
self.minimumZoomScale = minScale;
}
#pragma mark -
#pragma mark Methods called during rotation to preserve the zoomScale and the visible portion of the image
// returns the center point, in image coordinate space, to try to restore after rotation.
- (CGPoint)pointToCenterAfterRotation
{
CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
return [self convertPoint:boundsCenter toView:imageView];
}
// returns the zoom scale to attempt to restore after rotation.
- (CGFloat)scaleToRestoreAfterRotation
{
CGFloat contentScale = 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 (contentScale <= self.minimumZoomScale + FLT_EPSILON)
contentScale = 0;
return contentScale;
}
- (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;
}
// Adjusts content offset and scale to try to preserve the old zoomscale and center.
- (void)restoreCenterPoint:(CGPoint)oldCenter scale:(CGFloat)oldScale
{
// Step 1: restore zoom scale, first making sure it is within the allowable range.
self.zoomScale = MIN(self.maximumZoomScale, MAX(self.minimumZoomScale, oldScale));
// 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:oldCenter fromView: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];
offset.x = MAX(minOffset.x, MIN(maxOffset.x, offset.x));
offset.y = MAX(minOffset.y, MIN(maxOffset.y, offset.y));
self.contentOffset = offset;
}
@end