-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmoothView.m
More file actions
executable file
·246 lines (193 loc) · 7.3 KB
/
SmoothView.m
File metadata and controls
executable file
·246 lines (193 loc) · 7.3 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
//
// SmoothView.m
// AlphaPhotoNote
//
// Created by Juan Ribes on 20/04/13.
// Copyright (c) 2013 Juan Ribes. All rights reserved.
//
#import "SmoothView.h"
@interface SmoothView()
- (CGPoint)calculateMidPointForPoint:(CGPoint)p1 andPoint:(CGPoint)p2;
@end
@implementation SmoothView
@synthesize lastPoint;
@synthesize prePreviousPoint;
@synthesize previousPoint;
@synthesize lineWidth;
@synthesize colorPen = _colorPen;
@synthesize incrementalImage = _incrementalImage;
@synthesize bufferedImage = _bufferedImage;
-(void)configure
{
[self setMultipleTouchEnabled:NO];
[self setColorPen:[UIColor blueColor]];
_shouldClean = NO;
_beginTouch = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(replaceImage:)];
tap.numberOfTapsRequired = 3; // Tap three to clear drawing!
[self addGestureRecognizer:tap];
}
- (void) setIncrementalImage:(UIImage *)incrementalImage
{
_incrementalImage = incrementalImage;
self.image = incrementalImage;
}
-(void) setColorPen:(UIColor *)colorPen
{
if (colorPen != _colorPen)
_colorPen = colorPen;
}
- (UIColor *) colorPen
{
if (!_colorPen) {
return [UIColor blueColor];
} else {
return _colorPen;
}
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self configure];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self configure];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
/*- (void)drawRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
if (!self.incrementalImage) // first time; paint background white
{
[[UIColor whiteColor] setFill];
CGContextFillRect(context, self.bounds);
} else {
if ((!_shouldClean) && (!self.incrementalImage))
[[UIColor colorWithPatternImage:self.incrementalImage] setFill];
}
[self.incrementalImage drawInRect:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height)];
[self.colorPen setStroke];
CGContextStrokePath(context);
self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
self.previousPoint = [touch locationInView:self];
_beginTouch = YES;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
if (!self.incrementalImage) // first time; paint background white
{
[[UIColor whiteColor] setFill];
CGContextFillRect(context, self.bounds);
} else {
if ((!_shouldClean) && (!self.incrementalImage))
[[UIColor colorWithPatternImage:self.incrementalImage] setFill];
}
[self.incrementalImage drawInRect:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height)];
[self.colorPen setStroke];
CGContextStrokePath(context);
self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
self.prePreviousPoint = self.previousPoint;
self.previousPoint = [touch previousLocationInView:self];
CGPoint currentPoint = [touch locationInView:self];
// calculate mid point
CGPoint mid1 = [self calculateMidPointForPoint:self.previousPoint andPoint:self.prePreviousPoint];
CGPoint mid2 = [self calculateMidPointForPoint:currentPoint andPoint:self.previousPoint];
//from
// UIGraphicsBeginImageContext(self.incrementalImage.size);
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// UIGraphicsPushContext(context);
[[self colorPen] setStroke];
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), true);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
//[self.incrementalImage drawInRect:CGRectMake(0, 0, self.incrementalImage.size.width, self.incrementalImage.size.height)];
[self.incrementalImage drawInRect:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height)];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, self.previousPoint.x, self.previousPoint.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGFloat xDist = (previousPoint.x - currentPoint.x); //[2]
CGFloat yDist = (previousPoint.y - currentPoint.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]
distance = distance / 10;
if (distance > 10) {
distance = 10.0;
}
distance = distance / 10;
distance = distance * 3;
if (3.0 - distance > self.lineWidth) {
lineWidth = lineWidth + 0.3;
} else {
lineWidth = lineWidth - 0.3;
}
CGContextSetLineWidth(context, self.lineWidth);
CGContextStrokePath(context);
// UIGraphicsPopContext();
self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//to
}
- (CGPoint)calculateMidPointForPoint:(CGPoint)p1 andPoint:(CGPoint)p2 {
return CGPointMake((p1.x+p2.x)/2, (p1.y+p2.y)/2);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
[self setLineWidth:1.0];
if ([touch tapCount] == 1) {
// UIGraphicsBeginImageContext(self.incrementalImage.size);
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
// UIGraphicsPushContext(context);
[[self colorPen] setStroke];
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), true);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
// [self.incrementalImage drawInRect:CGRectMake(0, 0, self.incrementalImage.size.width, self.incrementalImage.size.height)];
[self.incrementalImage drawInRect:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height)];
CGContextMoveToPoint(context, currentPoint.x, currentPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 4.0);
CGContextStrokePath(context);
// UIGraphicsPopContext();
self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)trash
{
_shouldClean = YES;
self.incrementalImage = nil;
self.bufferedImage = nil;
[self setNeedsDisplay];
}
-(void) replaceImage:(UITapGestureRecognizer *)t
{
[self replaceImage];
}
-(void) replaceImage
{
self.incrementalImage = self.bufferedImage;
[self setNeedsDisplay];
}
@end