-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLRDStickerView.m
218 lines (158 loc) · 7.72 KB
/
CLRDStickerView.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
/*
Copyright (c) 2017 Coloridad Ltd.
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 "CLRDStickerView.h"
@interface CLRDStickerView () <UIGestureRecognizerDelegate>
@property NSMutableSet<UIGestureRecognizer *> *activeGestureRecognizers;
@property NSMutableDictionary<NSString *, NSValue *> *eventDictionary;
@end
@implementation CLRDStickerView
- (id)init {
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
_activeGestureRecognizers = [NSMutableSet set];
_eventDictionary = [NSMutableDictionary dictionary];
}
- (void)addSticker:(CLRDSticker *)sticker {
CGRect adjustedCenter = CGRectMake(self.center.x - (sticker.frame.size.width / 2),
self.center.y - (sticker.frame.size.height / 2),
sticker.frame.size.width,
sticker.frame.size.height);
[self addSticker:sticker withFrame:adjustedCenter];
}
- (void)addSticker:(CLRDSticker *)sticker withFrame:(CGRect)frame {
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[panRecognizer setDelegate:self];
[sticker addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[rotationRecognizer setDelegate:self];
[sticker addGestureRecognizer:rotationRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[pinchRecognizer setDelegate:self];
[sticker addGestureRecognizer:pinchRecognizer];
sticker.userInteractionEnabled = YES;
[self addSubview:sticker];
}
- (void)removeSticker:(CLRDSticker *)sticker {
if ([sticker.superview isEqual:self]) {
for (UIGestureRecognizer *recognizer in sticker.gestureRecognizers) {
[recognizer setDelegate:nil];
[sticker removeGestureRecognizer:recognizer];
}
sticker.userInteractionEnabled = NO;
[sticker removeFromSuperview];
}
}
- (void)handleGesture:(UIGestureRecognizer *)recognizer {
CLRDSticker *targetSticker = (CLRDSticker *)(recognizer.view);
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
if (_activeGestureRecognizers.count == 0) {
targetSticker.referenceTransform = targetSticker.transform;
}
[_activeGestureRecognizers addObject:recognizer];
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(stickerView:changeBeganForSticker:)] == YES) {
[self.delegate stickerView:self changeBeganForSticker:targetSticker];
}
break;
}
case UIGestureRecognizerStateEnded: {
targetSticker.referenceTransform = [self applyRecognizer:recognizer toTransform:targetSticker.referenceTransform];
[_activeGestureRecognizers removeObject:recognizer];
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(stickerView:changeEndedForSticker:)] == YES) {
[self.delegate stickerView:self changeEndedForSticker:targetSticker];
}
break;
}
case UIGestureRecognizerStateChanged: {
CGAffineTransform transform = targetSticker.referenceTransform;
for (UIGestureRecognizer *recognizer in _activeGestureRecognizers) {
transform = [self applyRecognizer:recognizer toTransform:transform];
}
targetSticker.transform = transform;
break;
}
default:
break;
}
for (NSString *event in _eventDictionary.allKeys) {
CGRect frame = [_eventDictionary objectForKey:event].CGRectValue;
BOOL eventDetected = CGRectIntersectsRect(frame, targetSticker.frame);
if (eventDetected == YES && [targetSticker.associatedEvents containsObject:event] == NO) {
[targetSticker.associatedEvents addObject:event];
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(stickerView:eligibilityBeganFor:forSticker:)]) {
[self.delegate stickerView:self eligibilityBeganFor:event forSticker:targetSticker];
}
}
else if (eventDetected == NO && [targetSticker.associatedEvents containsObject:event] == YES) {
[targetSticker.associatedEvents removeObject:event];
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(stickerView:eligibilityEndedFor:forSticker:)]) {
[self.delegate stickerView:self eligibilityEndedFor:event forSticker:targetSticker];
}
}
}
}
- (CGAffineTransform)applyRecognizer:(UIGestureRecognizer *)recognizer toTransform:(CGAffineTransform)transform {
if ([recognizer respondsToSelector:@selector(translationInView:)]) {
CGPoint translation = [(UIPanGestureRecognizer *)recognizer translationInView:self];
return CGAffineTransformTranslate(transform, translation.x, translation.y);
}
else if ([recognizer respondsToSelector:@selector(rotation)]) {
return CGAffineTransformRotate(transform, [(UIRotationGestureRecognizer *)recognizer rotation]);
}
else if ([recognizer respondsToSelector:@selector(scale)]) {
CGFloat scale = [(UIPinchGestureRecognizer *)recognizer scale];
return CGAffineTransformScale(transform, scale, scale);
}
else {
return transform;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)setFrame:(CGRect)frame forEvent:(NSString *)event {
[_eventDictionary setObject:[NSValue valueWithCGRect:frame] forKey:event];
}
- (BOOL)frameIntersects:(CGRect)frame withFrameForEvent:(NSString *)event {
CGRect eventFrame = [_eventDictionary objectForKey:event].CGRectValue;
return CGRectIntersectsRect(frame, eventFrame);
}
- (void)removeFrameForEvent:(NSString *)event {
[_eventDictionary removeObjectForKey:event];
}
@end