/
DragView.m
90 lines (73 loc) · 2.73 KB
/
DragView.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
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook, 5.x Edition
BSD License, Use at your own risk
*/
#import "DragView.h"
@implementation DragView
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Promote the touched view
[self.superview bringSubviewToFront:self];
// initialize translation offsets
tx = self.transform.tx;
ty = self.transform.ty;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch.tapCount == 3)
{
// Reset geometry upon double-tap
self.transform = CGAffineTransformIdentity;
tx = 0.0f; ty = 0.0f; scale = 1.0f; theta = 0.0f;
}
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (void) updateTransformWithOffset: (CGPoint) translation
{
// Create a blended transform representing translation, rotation, and scaling
self.transform = CGAffineTransformMakeTranslation(translation.x + tx, translation.y + ty);
self.transform = CGAffineTransformRotate(self.transform, theta);
self.transform = CGAffineTransformScale(self.transform, scale, scale);
NSLog(@"Xscale: %f YScale: %f Rotation: %f", self.xscale, self.yscale, self.rotation * (180 / M_PI));
}
- (void) handlePan: (UIPanGestureRecognizer *) uigr
{
CGPoint translation = [uigr translationInView:self.superview];
[self updateTransformWithOffset:translation];
}
- (void) handleRotation: (UIRotationGestureRecognizer *) uigr
{
theta = uigr.rotation;
[self updateTransformWithOffset:CGPointZero];
}
- (void) handlePinch: (UIPinchGestureRecognizer *) uigr
{
scale = uigr.scale;
[self updateTransformWithOffset:CGPointZero];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (id) initWithImage:(UIImage *)image
{
// Initialize and set as touchable
if (!(self = [super initWithImage:image])) return self;
self.userInteractionEnabled = YES;
// Reset geometry to identities
self.transform = CGAffineTransformIdentity;
tx = 0.0f; ty = 0.0f; scale = 1.0f; theta = 0.0f;
// Add gesture recognizer suite
UIRotationGestureRecognizer *rot = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.gestureRecognizers = [NSArray arrayWithObjects: rot, pinch, pan, nil];
for (UIGestureRecognizer *recognizer in self.gestureRecognizers) recognizer.delegate = self;
return self;
}
@end