Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#include "common.h"
2
#import "MuInkView.h"
3
4
@implementation MuInkView
5
{
6
CGSize pageSize;
7
NSMutableArray *curves;
8
UIColor *color;
9
}
10
11
- (id) initWithPageSize:(CGSize)_pageSize
12
{
13
self = [super initWithFrame:CGRectMake(0, 0, 100, 100)];
14
if (self) {
15
[self setOpaque:NO];
16
pageSize = _pageSize;
17
color = [[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] retain];
18
curves = [[NSMutableArray array] retain];
19
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onDrag:)];
20
[self addGestureRecognizer:rec];
21
[rec release];
22
}
23
return self;
24
}
25
26
@synthesize curves;
27
28
-(void)dealloc
29
{
30
[curves release];
31
[color release];
32
[super dealloc];
33
}
34
35
-(void) onDrag:(UIPanGestureRecognizer *)rec
36
{
37
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
38
CGPoint p = [rec locationInView:self];
39
p.x /= scale.width;
40
p.y /= scale.height;
41
42
if (rec.state == UIGestureRecognizerStateBegan)
43
[curves addObject:[NSMutableArray array]];
44
45
NSMutableArray *curve = [curves lastObject];
46
[curve addObject:[NSValue valueWithCGPoint:p]];
47
48
[self setNeedsDisplay];
49
}
50
51
- (void)drawRect:(CGRect)rect
52
{
53
CGSize scale = fitPageToScreen(pageSize, self.bounds.size);
54
CGContextRef cref = UIGraphicsGetCurrentContext();
55
CGContextScaleCTM(cref, scale.width, scale.height);
56
57
[color set];
58
CGContextSetLineWidth(cref, 5.0);
59
60
for (NSArray *curve in curves)
61
{
62
if (curve.count >= 2)
63
{
64
CGPoint pt = [[curve objectAtIndex:0] CGPointValue];
65
CGContextBeginPath(cref);
66
CGContextMoveToPoint(cref, pt.x, pt.y);
67
CGPoint lpt = pt;
68
69
for (int i = 1; i < curve.count; i++)
70
{
71
pt = [[curve objectAtIndex:i] CGPointValue];
72
CGContextAddQuadCurveToPoint(cref, lpt.x, lpt.y, (pt.x + lpt.x)/2, (pt.y + lpt.y)/2);
73
lpt = pt;
74
}
75
76
CGContextAddLineToPoint(cref, pt.x, pt.y);
77
CGContextStrokePath(cref);
78
}
79
}
80
}
81
82
@end
83
84