-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathGestureView.m
120 lines (100 loc) · 3.2 KB
/
GestureView.m
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
//
// GestureView.m
// Gestures
//
// Created by Adam Preble on 4/27/09.
// Copyright 2010 Giraffe Lab. All rights reserved.
//
#import "GestureView.h"
#import "GLGestureRecognizer.h"
#import "GLGestureRecognizer+JSONTemplates.h"
@interface GestureView () {
GLGestureRecognizer *recognizer;
CGPoint center;
float score, angle;
}
@property (nonatomic, copy) NSString *caption;
@end
@implementation GestureView
@synthesize caption;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
recognizer = [[GLGestureRecognizer alloc] init];
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Gestures" ofType:@"json"]];
BOOL ok;
NSError *error;
ok = [recognizer loadTemplatesFromJsonData:jsonData error:&error];
if (!ok)
{
NSLog(@"Error loading gestures: %@", error);
self.caption = @"Error loading gestures.";
return;
}
self.caption = @"";
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
CGContextFillRect(ctx, rect);
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
CGContextSelectFont(ctx, "Helvetica", 20, kCGEncodingMacRoman);
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1, -1));
CGContextShowTextAtPoint(ctx, 10, 30, [caption UTF8String], [caption length]);
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
for (NSValue *pointValue in [recognizer touchPoints])
{
CGPoint pointInView = [pointValue CGPointValue];
if (pointValue == [[recognizer touchPoints] objectAtIndex:0])
CGContextMoveToPoint(ctx, pointInView.x, pointInView.y);
else
CGContextAddLineToPoint(ctx, pointInView.x, pointInView.y);
}
CGContextStrokePath(ctx);
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
for (NSValue *pointValue in [recognizer resampledPoints])
{
CGPoint pointInView = [pointValue CGPointValue];
pointInView.x = pointInView.x * 100.0f + 160.0f;
pointInView.y = pointInView.y * 100.0f + 240.0f;
if (pointValue == [[recognizer resampledPoints] objectAtIndex:0])
CGContextMoveToPoint(ctx, pointInView.x, pointInView.y);
else
CGContextAddLineToPoint(ctx, pointInView.x, pointInView.y);
}
CGContextStrokePath(ctx);
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextFillRect(ctx, CGRectMake(center.x, center.y, 4.0f, 4.0f));
}
- (void)processGestureData
{
NSString *gestureName = [recognizer findBestMatchCenter:¢er angle:&angle score:&score];
self.caption = [NSString stringWithFormat:@"%@ (%0.2f, %d)", gestureName, score, (int)(360.0f*angle/(2.0f*M_PI))];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[recognizer resetTouches];
[recognizer addTouches:touches fromView:self];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[recognizer addTouches:touches fromView:self];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[recognizer addTouches:touches fromView:self];
[self processGestureData];
[self setNeedsDisplay];
}
@end