forked from Public-Computing-Lab/Cycle-Atlanta-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImageViewResizable.m
More file actions
executable file
·158 lines (129 loc) · 5.67 KB
/
UIImageViewResizable.m
File metadata and controls
executable file
·158 lines (129 loc) · 5.67 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
/** Reno Tracks, Copyright 2012, 2013 Hack4Reno
*
* @author Brad.Hellyar <bradhellyar@gmail.com>
*
* Updated/Modified for Reno, Nevada app deployment. Based on the
* CycleTracks codebase for SFCTA, and the Atlanta Cycle app repo.
*
* You should have received a copy of the GNU General Public License
* along with Reno Cycle. 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