Created
September 28, 2013 13:42
-
-
Save heyfluke/6742196 to your computer and use it in GitHub Desktop.
Smoothed drawing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SmoothedBIView.h" | |
#import "TouchProfiler.h" | |
@implementation SmoothedBIView | |
{ | |
UIBezierPath *path; | |
UIImage *incrementalImage; | |
CGPoint pts[5]; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment | |
uint ctr; | |
TouchProfiler *_profiler; | |
} | |
//- (id)initWithCoder:(NSCoder *)aDecoder | |
//{ | |
// if (self = [super initWithCoder:aDecoder]) | |
// { | |
// [self setMultipleTouchEnabled:NO]; | |
// [self setBackgroundColor:[UIColor whiteColor]]; | |
// path = [UIBezierPath bezierPath]; | |
// [path setLineWidth:2.0]; | |
// } | |
// return self; | |
//} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
if (self = [super initWithFrame:frame]) | |
{ | |
_profiler = new TouchProfiler(); // release | |
[self setMultipleTouchEnabled:NO]; | |
[self setBackgroundColor:[UIColor whiteColor]]; | |
path = [UIBezierPath bezierPath]; | |
[path setLineWidth:2.0]; | |
} | |
return self; | |
} | |
/*- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self setMultipleTouchEnabled:NO]; | |
path = [UIBezierPath bezierPath]; | |
[path setLineWidth:2.0]; | |
} | |
return self; | |
}*/ | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect | |
{ | |
[incrementalImage drawInRect:rect]; | |
[path stroke]; | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
_profiler->addPoint(); | |
ctr = 0; | |
UITouch *touch = [touches anyObject]; | |
pts[0] = [touch locationInView:self]; | |
} | |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
_profiler->addPoint(); | |
UITouch *touch = [touches anyObject]; | |
CGPoint p = [touch locationInView:self]; | |
ctr++; | |
pts[ctr] = p; | |
if (ctr == 4) | |
{ | |
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment | |
[path moveToPoint:pts[0]]; | |
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2] | |
[self setNeedsDisplay]; | |
// replace points and get ready to handle the next segment | |
pts[0] = pts[3]; | |
pts[1] = pts[4]; | |
ctr = 1; | |
} | |
} | |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
_profiler->addEnd(); | |
[self drawBitmap]; | |
[self setNeedsDisplay]; | |
[path removeAllPoints]; | |
ctr = 0; | |
} | |
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event | |
{ | |
[self touchesEnded:touches withEvent:event]; | |
} | |
- (void)drawBitmap | |
{ | |
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); | |
if (!incrementalImage) // first time; paint background white | |
{ | |
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; | |
[[UIColor whiteColor] setFill]; | |
[rectpath fill]; | |
} | |
[incrementalImage drawAtPoint:CGPointZero]; | |
[[UIColor blackColor] setStroke]; | |
[path stroke]; | |
incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment