forked from PhillyOpen/Cycle-Philly-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImageViewResizable.m
177 lines (148 loc) · 6.5 KB
/
UIImageViewResizable.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
/** Cycle Philly, 2013 Code For Philly
* Philadelphia, PA. USA
*
*
* Contact: Corey Acri <[email protected]>
* Lloyd Emelle <[email protected]>
*
* Updated/Modified for Philadelphia's app deployment. Based on the
* Cycle Atlanta and CycleTracks codebase for SFCTA.
*
* Cycle Atlanta, Copyright 2012, 2013 Georgia Institute of Technology
* Atlanta, GA. USA
*
* @author Christopher Le Dantec <[email protected]>
* @author Anhong Guo <[email protected]>
*
* Cycle Atlanta is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cycle Atlanta is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cycle Atlanta. If not, see <http://www.gnu.org/licenses/>.
*/
//
// UIImageViewResizable.m
//
// Created by Mike Valstar on 2012-09-10.
//
#import "UIImageViewResizable.h"
#define MINIMUM_SCALE 1.0
#define MAXIMUM_SCALE 4.0
@implementation UIImageViewResizable
@synthesize isZoomable;
/********************
Public Methods
*******************/
-(void) scaleToMinimum{
CGAffineTransform transform = CGAffineTransformMakeScale(MINIMUM_SCALE, MINIMUM_SCALE);
self.transform = transform;
//check center and move if outside bounds
CGPoint newCenter = CGPointMake(self.frame.origin.x + (self.frame.size.width / 2),
self.frame.origin.y + (self.frame.size.height / 2));
newCenter = [self constrictToBounds:newCenter];
self.frame = CGRectMake(newCenter.x - (self.frame.size.width / 2),
newCenter.y - (self.frame.size.height / 2),
self.frame.size.width,
self.frame.size.height);
[self removeGestureRecognizer:panGesture]; // remove pan gesture when zoomed out
}
-(void) applyGestures{
self.userInteractionEnabled = YES;
// Pinch
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(pinch:)];
pgr.delegate = self;
[self addGestureRecognizer:pgr];
// Pan
panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(pan:)];
panGesture.delegate = self;
//[self addGestureRecognizer:panGesture]; // Do not add right away wait until zoomed
// Double Tap
UITapGestureRecognizer *dtgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(doubleTap:)];
dtgr.numberOfTapsRequired = 2;
dtgr.delegate = self;
[self addGestureRecognizer:dtgr];
}
/********************
Gestures
*******************/
- (void)pinch:(UIPinchGestureRecognizer *)gesture {
if (isZoomable == NO) return;
if (gesture.state == UIGestureRecognizerStateEnded
|| gesture.state == UIGestureRecognizerStateChanged) {
CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
CGFloat newScale = currentScale * gesture.scale;
[self addGestureRecognizer:panGesture];
if (newScale < MINIMUM_SCALE) {
newScale = MINIMUM_SCALE;
[self removeGestureRecognizer:panGesture]; // remove pan gesture when zoomed out
}
if (newScale > MAXIMUM_SCALE) {
newScale = MAXIMUM_SCALE;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.transform = transform;
gesture.scale = 1;
//check center and move if outside bounds
CGPoint newCenter = CGPointMake(self.frame.origin.x + (self.frame.size.width / 2),
self.frame.origin.y + (self.frame.size.height / 2));
newCenter = [self constrictToBounds:newCenter];
self.frame = CGRectMake(newCenter.x - (self.frame.size.width / 2),
newCenter.y - (self.frame.size.height / 2),
self.frame.size.width,
self.frame.size.height);
}
}
- (void)pan:(UIPanGestureRecognizer *)gesture {
if (isZoomable == NO) return;
CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
CGPoint translation = [gesture translationInView:self];
CGPoint newCenter = CGPointMake(gesture.view.center.x + (translation.x * currentScale),
gesture.view.center.y + (translation.y * currentScale));
newCenter = [self constrictToBounds:newCenter];
gesture.view.center = newCenter;
[gesture setTranslation:CGPointMake(0, 0) inView:self];
}
- (void)doubleTap:(UITapGestureRecognizer *)gesture {
NSLog(@"double tap detected");
if (isZoomable == NO) return;
CGFloat currentScale = self.frame.size.width / self.bounds.size.width;
if(ceil(currentScale) != ceil(MAXIMUM_SCALE)){
CGAffineTransform transform = CGAffineTransformMakeScale(MAXIMUM_SCALE, MAXIMUM_SCALE);
self.transform = transform;
[self addGestureRecognizer:panGesture];
}else{
CGAffineTransform transform = CGAffineTransformMakeScale(MINIMUM_SCALE, MINIMUM_SCALE);
self.transform = transform;
[self removeGestureRecognizer:panGesture]; // remove pan gesture when zoomed out
}
}
/********************
Internal Methods
*******************/
// Check the bounding box for the resize point
- (CGPoint)constrictToBounds:(CGPoint)point{
CGFloat lowerXBound = ceil(self.bounds.size.width - ( self.frame.size.width / 2 ));
CGFloat higherXBound = floor(lowerXBound + (self.frame.size.width - self.bounds.size.width));
CGFloat lowerYBound = ceil(self.bounds.size.height - ( self.frame.size.height / 2 ));
CGFloat higherYBound = floor(lowerYBound + (self.frame.size.height - self.bounds.size.height));
if ( point.x < lowerXBound)
point.x = lowerXBound;
if ( point.x > higherXBound )
point.x = higherXBound;
if ( point.y < lowerYBound)
point.y = lowerYBound;
if ( point.y > higherYBound )
point.y = higherYBound;
return point;
}
@end