From 2c246182096ca8f3e42d260cd93500ce2b171e09 Mon Sep 17 00:00:00 2001 From: Torin Nguyen Date: Thu, 5 Jul 2012 17:26:06 +0800 Subject: [PATCH] Added drawing code --- WTMGlyphDemo/WTMGlyphDetectorView.h | 1 + WTMGlyphDemo/WTMGlyphDetectorView.m | 45 ++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/WTMGlyphDemo/WTMGlyphDetectorView.h b/WTMGlyphDemo/WTMGlyphDetectorView.h index 99ffc4c..c359127 100644 --- a/WTMGlyphDemo/WTMGlyphDetectorView.h +++ b/WTMGlyphDemo/WTMGlyphDetectorView.h @@ -18,6 +18,7 @@ @interface WTMGlyphDetectorView : UIView @property (nonatomic, strong) id delegate; +@property (nonatomic, assign) BOOL enableDrawing; - (void)loadTemplatesWithNames:(NSString*)firstTemplate, ... NS_REQUIRES_NIL_TERMINATION; diff --git a/WTMGlyphDemo/WTMGlyphDetectorView.m b/WTMGlyphDemo/WTMGlyphDetectorView.m index 667074e..4e51b1a 100644 --- a/WTMGlyphDemo/WTMGlyphDetectorView.m +++ b/WTMGlyphDemo/WTMGlyphDetectorView.m @@ -11,10 +11,13 @@ @interface WTMGlyphDetectorView() @property (nonatomic, strong) WTMGlyphDetector *glyphDetector; @property (nonatomic, strong) NSMutableArray *glyphNamesArray; +@property (nonatomic, strong) UIBezierPath *myPath; @end @implementation WTMGlyphDetectorView @synthesize delegate; +@synthesize myPath; +@synthesize enableDrawing; @synthesize glyphDetector; @synthesize glyphNamesArray; @@ -24,6 +27,14 @@ - (id)initWithFrame:(CGRect)frame self = [super initWithFrame:frame]; if (self) { [self initGestureDetector]; + + self.backgroundColor = [UIColor clearColor]; + self.enableDrawing = YES; + + self.myPath = [[UIBezierPath alloc]init]; + self.myPath.lineCapStyle = kCGLineCapRound; + self.myPath.miterLimit = 0; + self.myPath.lineWidth = 10; } return self; } @@ -96,8 +107,14 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //This is basically the content of resetIfTimeout BOOL hasTimeOut = [self.glyphDetector hasTimedOut]; if (hasTimeOut) { - [self.glyphDetector reset]; NSLog(@"Gesture detector reset"); + [self.glyphDetector reset]; + + if (self.enableDrawing) { + [self.myPath removeAllPoints]; + //This is not recommended for production, but it's ok here since we don't have a lot to draw + [self setNeedsDisplay]; + } } UITouch *touch = [touches anyObject]; @@ -105,6 +122,11 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event [self.glyphDetector addPoint:point]; [super touchesBegan:touches withEvent:event]; + + if (!self.enableDrawing) + return; + + [self.myPath moveToPoint:point]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event @@ -114,6 +136,14 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event [self.glyphDetector addPoint:point]; [super touchesMoved:touches withEvent:event]; + + if (!self.enableDrawing) + return; + + [self.myPath addLineToPoint:point]; + + //This is not recommended for production, but it's ok here since we don't have a lot to draw + [self setNeedsDisplay]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event @@ -126,4 +156,17 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event [super touchesEnded:touches withEvent:event]; } + +- (void)drawRect:(CGRect)rect +{ + [super drawRect:rect]; + + if (!self.enableDrawing) + return; + + [[UIColor whiteColor] setStroke]; + [self.myPath strokeWithBlendMode:kCGBlendModeNormal alpha:0.5]; +} + + @end