forked from phoenixbull/ZoomRotatePanImageView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoomRotatePanImageView.m
executable file
·134 lines (108 loc) · 3.92 KB
/
ZoomRotatePanImageView.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
//
// ZoomRotatePanImageView.m
//
//
// Created by bennythemink on 20/07/12.
// Copyright (c) 2012 bennythemink. All rights reserved.
//
#import "ZoomRotatePanImageView.h"
@interface ZoomRotatePanImageView ()
- (void) setUpGestures;
- (IBAction) handlePinch:(UIPinchGestureRecognizer*)recogniser;
- (IBAction) handleRotate:(UIRotationGestureRecognizer*)recogniser;
- (IBAction) handlePan:(UIPanGestureRecognizer*)recogniser;
- (IBAction) handleTap:(UITapGestureRecognizer*)recogniser;
@end
@implementation ZoomRotatePanImageView
#pragma mark - Initialisation Overrides
- (id) initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
self = [super initWithImage:image highlightedImage:highlightedImage];
if (self) {
[self setUpGestures];
}
return self;
}
- (id) initWithImage:(UIImage *)image {
self = [super initWithImage:image];
if (self) {
[self setUpGestures];
}
return self;
}
- (id) init {
self = [super init];
if (self) {
[self setUpGestures];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpGestures];
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setUpGestures];
}
return self;
}
#pragma mark - Utility Methods
- (void) setUpGestures {
[self setUserInteractionEnabled:TRUE];
_pinchRecogniser = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
_rotateRecogniser = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
_panRecogniser = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
_tapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[_pinchRecogniser setDelegate:self];
[_rotateRecogniser setDelegate:self];
[_panRecogniser setDelegate:self];
[_tapRecogniser setDelegate:self];
[self addGestureRecognizer:_pinchRecogniser];
[self addGestureRecognizer:_rotateRecogniser];
[self addGestureRecognizer:_panRecogniser];
[self addGestureRecognizer:_tapRecogniser];
// set the aspect ratio mode
[self setContentMode:UIViewContentModeScaleAspectFit];
}
- (IBAction) handlePinch:(UIPinchGestureRecognizer*)recogniser {
recogniser.view.transform = CGAffineTransformScale(recogniser.view.transform, recogniser.scale, recogniser.scale);
recogniser.scale = 1;
}
- (IBAction) handleRotate:(UIRotationGestureRecognizer*)recogniser {
recogniser.view.transform = CGAffineTransformRotate(recogniser.view.transform, recogniser.rotation);
recogniser.rotation = 0;
}
- (IBAction) handleTap:(UITapGestureRecognizer*)recogniser {
[self resetWithAnimation:TRUE];
}
- (IBAction) handlePan:(UIPanGestureRecognizer*)recogniser {
if (recogniser.state == UIGestureRecognizerStateBegan || recogniser.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recogniser translationInView:self.superview];
CGPoint translatedCenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
[self setCenter:translatedCenter];
[recogniser setTranslation:CGPointZero inView:self];
}
}
- (void) reset {
self.transform = CGAffineTransformIdentity;
self.center = self.superview.center;
}
- (void) resetWithAnimation:(BOOL)animation {
if (!animation) [self reset];
else {
[UIView animateWithDuration:.25 animations:^{
self.transform = CGAffineTransformIdentity;
self.center = self.superview.center;
}];
}
}
#pragma mark - UIGestureRecognizer Delegate Methods
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return TRUE;
}
@end