public
Description: A Demo of the iPhone's CGPathAddQuadCurveToPoint function
Homepage:
Clone URL: git://github.com/below/iphone-bezierdemo.git
Alexander v. Below (author)
Sat Nov 08 03:16:34 -0800 2008
iphone-bezierdemo / Classes / BDBezierView.m
100644 73 lines (57 sloc) 1.747 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
//
// BDBezierView.m
// BezierDemo
//
// Created by Alexander v. Below on 07.11.08.
// Copyright 2008 AVB Software. All rights reserved.
//
 
#import "BDBezierView.h"
 
 
@implementation BDBezierView
 
- (id)initWithCoder:(NSCoder *)aCoder {
if (self = [super initWithCoder:aCoder])
{
[self setUpView];
}
return self;
}
 
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
[self setUpView];
}
    return self;
}
 
 
- (void) setUpView {
CGFloat w = self.bounds.size.width;
// CGFloat h = self.bounds.size.height;
 
startView = [[BDPointView alloc] initWithFrame:CGRectMake(20, 200, 40, 40) color:[UIColor redColor]];
endView = [[BDPointView alloc] initWithFrame:CGRectMake(w-60, 200, 40, 40) color:[UIColor redColor]];
controlView = [[BDPointView alloc] initWithFrame:CGRectMake(w/2-20, 100, 40, 40) color:[UIColor blueColor]];
 
[self addSubview:startView];
[self addSubview:endView];
[self addSubview:controlView];
}
 
- (void)drawRect:(CGRect)rect {
CGContextRef ctx;
ctx = UIGraphicsGetCurrentContext();
 
CGMutablePathRef bezierPath = CGPathCreateMutable();
 
CGPathMoveToPoint(bezierPath, nil, startView.center.x, startView.center.y);
CGPathAddQuadCurveToPoint(bezierPath, nil, controlView.center.x, controlView.center.y, endView.center.x, endView.center.y);
CGContextAddPath(ctx, bezierPath);
CGContextStrokePath(ctx);
CFRelease(bezierPath);
}
 
- (void)dealloc {
    [super dealloc];
}
 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 
for (UITouch * touch in touches)
{
if ([[touch view] isKindOfClass:[BDPointView self]]) {
CGPoint location = [touch locationInView:self];
[touch view].center = location;
[self setNeedsDisplay];
return;
}
}
}
@end