Repository navigation
Expand file tree
/
Copy pathWDGradientEditor.m
More file actions
282 lines (221 loc) · 8.36 KB
/
Copy pathWDGradientEditor.m
File metadata and controls
282 lines (221 loc) · 8.36 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// WDGradientEditor.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2010-2013 Steve Sprang
//
#import "WDGradientEditor.h"
#import "WDGradient.h"
#import "WDGradientController.h"
#import "WDGradientStop.h"
#import "WDGradientStopIndicator.h"
#import "WDUtilities.h"
#import "UIView+Additions.h"
#define kMaxAllowableStops 6
#define kRemoveDistance 50
@implementation WDGradientEditor
@synthesize gradient = gradient_;
@synthesize renderingGradient = renderingGradient_;
@synthesize controller = controller_;
@synthesize inactive = inactive_;
- (void) awakeFromNib
{
indicators_ = [[NSMutableArray alloc] init];
self.gradient = [WDGradient defaultGradient];
// add a subview to register for gestures
UIView *gestureView = [[UIView alloc] initWithFrame:self.bounds];
gestureView.opaque = NO;
gestureView.backgroundColor = nil;
[self addSubview:gestureView];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipe.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
[gestureView addGestureRecognizer:swipe];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
tap.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap];
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tap.numberOfTapsRequired = 1;
[gestureView addGestureRecognizer:tap];
self.gradient = [WDGradient defaultGradient];
}
- (void) handleSingleTap:(UISwipeGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
if (inactive_) {
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
- (void) handleDoubleTap:(UISwipeGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
[controller_ distributeGradientStops:self];
}
}
- (void) handleSwipe:(UISwipeGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
[controller_ reverseGradient:self];
}
}
- (void) positionIndicator:(WDGradientStopIndicator *)indicator
{
CGRect bounds = CGRectInset(self.bounds, 1, 0);
indicator.sharpCenter = CGPointMake(CGRectGetMinX(bounds) + indicator.stop.ratio * CGRectGetWidth(bounds),
CGRectGetHeight(bounds) + (CGRectGetHeight(indicator.bounds) / 2.0f) - 5);
}
- (void) setGradient:(WDGradient *)gradient
{
if ([gradient_ isEqual:gradient]) {
// we can bail early, but we still need to make sure the color controller is showing the appropriate color
if (activeIndicator_) {
[controller_ colorSelected:activeIndicator_.stop.color];
}
return;
}
gradient_ = gradient;
float activeIndicatorRatio = -1;
if (activeIndicator_) {
activeIndicatorRatio = activeIndicator_.stop.ratio;
activeIndicator_ = nil;
}
[indicators_ makeObjectsPerformSelector:@selector(removeFromSuperview)];
[indicators_ removeAllObjects];
for (WDGradientStop *stop in gradient.stops) {
WDGradientStopIndicator *indicator = [[WDGradientStopIndicator alloc] initWithStop:stop];
[indicators_ addObject:indicator];
[self addSubview:indicator];
[self positionIndicator:indicator];
if (stop.ratio == activeIndicatorRatio) {
[self setActiveIndicator:indicator];
}
indicator.hidden = inactive_;
}
self.renderingGradient = [gradient copy];
if (!activeIndicator_) {
[self setActiveIndicator:indicators_[0]];
}
}
- (void) setRenderingGradient:(WDGradient *)gradient
{
renderingGradient_ = gradient;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
WDDrawCheckersInRect(ctx, rect, 7);
CGContextDrawLinearGradient(ctx,
[renderingGradient_ gradientRef], self.bounds.origin, CGPointMake(CGRectGetMaxX(self.bounds), 0),
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
[[UIColor blackColor] set];
UIRectFrame(self.bounds);
}
- (void) setInactive:(BOOL)inactive
{
inactive_ = inactive;
for (WDGradientStopIndicator *indicator in indicators_) {
indicator.hidden = inactive;
}
if (!inactive_ && !activeIndicator_) {
[self setActiveIndicator:indicators_[0]];
}
}
- (NSArray *) stops
{
NSMutableArray *stops = [NSMutableArray array];
for (WDGradientStopIndicator *indicator in indicators_) {
[stops addObject:indicator.stop];
}
return stops;
}
- (WDGradientStopIndicator *) stopIndicatorWithRatio:(float)ratio
{
for (WDGradientStopIndicator *indicator in indicators_) {
if (indicator.stop.ratio == ratio) {
return indicator;
}
}
return nil;
}
- (void) setColor:(WDColor *)color
{
activeIndicator_.stop = [WDGradientStop stopWithColor:color andRatio:activeIndicator_.stop.ratio];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void) setActiveIndicator:(WDGradientStopIndicator *)indicator
{
activeIndicator_.selected = NO;
activeIndicator_ = nil;
activeIndicator_ = indicator;
activeIndicator_.selected = YES;
if (activeIndicator_) {
[controller_ colorSelected:activeIndicator_.stop.color];
}
}
- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
if (inactive_) {
return [super beginTrackingWithTouch:touch withEvent:event];
}
CGPoint pt = [touch locationInView:self];
moved_ = NO;
indicatorToDrag_ = indicatorToRemove_ = nil;
for (WDGradientStopIndicator *indicator in [indicators_ reverseObjectEnumerator]) {
if (CGRectContainsPoint(indicator.frame, pt)) {
[self setActiveIndicator:indicator];
indicatorToDrag_ = indicator;
break;
}
}
return [super beginTrackingWithTouch:touch withEvent:event];
}
- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
if (inactive_) {
return [super continueTrackingWithTouch:touch withEvent:event];
}
CGPoint pt = [touch locationInView:self];
moved_ = YES;
if (indicatorToDrag_) {
if ([indicators_ count] > 2 && fabs(indicatorToDrag_.center.y - pt.y) > kRemoveDistance) {
indicatorToDrag_.alpha = 0;
indicatorToRemove_ = indicatorToDrag_;
} else {
if (indicatorToRemove_) {
indicatorToDrag_.alpha = 1;
indicatorToRemove_ = nil;
}
indicatorToDrag_.stop = [WDGradientStop stopWithColor:indicatorToDrag_.stop.color andRatio:(pt.x / CGRectGetWidth(self.bounds))];
[self positionIndicator:indicatorToDrag_];
}
self.renderingGradient = [self.renderingGradient gradientWithStops:self.stops];
}
return [super continueTrackingWithTouch:touch withEvent:event];
}
- (void) endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
{
if (!inactive_ && !moved_ && !indicatorToDrag_ && (self.gradient.stops.count < kMaxAllowableStops)) {
CGPoint pt = [touch locationInView:self];
float ratio = (pt.x / CGRectGetWidth(self.bounds));
self.gradient = [self.gradient gradientWithStopAtRatio:ratio];
[self setActiveIndicator:[self stopIndicatorWithRatio:ratio]];
} else if (indicatorToRemove_) {
NSInteger ix = [indicators_ indexOfObject:indicatorToRemove_];
[indicators_ removeObject:indicatorToRemove_];
[indicatorToRemove_ removeFromSuperview];
activeIndicator_ = indicatorToRemove_ = nil;
ix--;
if (ix < 0) {
ix++;
}
[self setActiveIndicator:indicators_[ix]];
}
[self sendActionsForControlEvents:UIControlEventValueChanged];
return [super endTrackingWithTouch:touch withEvent:event];
}
@end