-
Notifications
You must be signed in to change notification settings - Fork 5
/
MultiTouchS7GraphView.m
149 lines (123 loc) · 3.19 KB
/
MultiTouchS7GraphView.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// MultiTouchS7GraphView.m
// created by Kiran Ryali
// kiranryali.com
//
//
#import "MultiTouchS7GraphView.h"
@implementation MultiTouchS7GraphView
/*
THIS CLASS SEPARATES THE TOUCH RESPONDERS FROM THE S7GRAPHVIEW
SO THAT IT CAN RESPOND TO UICONTROL UITOUCHEVENTS NORMALLY, WHILE THIS ONE HANDLES
MULTI TOUCH GESTURES
*/
- (id)init
{
if( self = [super init])
currentTouches = [[NSMutableArray alloc] init];
return self;
}
#pragma mark -
#pragma mark MultiTouch Functions
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray * array = [touches allObjects];
if([array count] == 1){
//NSLog(@"Single touch event began!");
UITouch * touch = [array objectAtIndex:0];
CGPoint point = [touch locationInView:self];
self.leftLineX = point.x;
[currentTouches addObject:touch];
//NSLog(@"currentTouches+: %d", [currentTouches count]);
[self setNeedsDisplay];
}
else if([array count] == 2){
CGFloat max = 0.0f;
CGFloat min = 0.0f;
//NSLog(@"Multi touch event began!");
for( int i = 0; i < [array count]; i++){
UITouch * touch = [array objectAtIndex:i];
[currentTouches addObject:touch];
//NSLog(@"currentTouches+: %d", [currentTouches count]);
CGPoint point = [touch locationInView:self];
if(i==0){
max = point.x;
min = point.x;
} else{
if(point.x > max){
max = point.x;
}
else if(point.x < min) {
min = point.x;
}
}
}
self.leftLineX = min;
self.rightLineX = max;
//NSLog(@"Touch %f, %f", max, min);
[self setNeedsDisplay];
}
}
- (void)printCurrentTouches{
for(int i=0; i< [currentTouches count]; i++){
UITouch * touch = [currentTouches objectAtIndex:i];
CGPoint newTouch = [touch locationInView:self];
//NSLog(@"%d:%f\n", i, newTouch.x);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGFloat max = 0.0f;
CGFloat min = 0.0f;
for( int i = 0; i < [currentTouches count]; i++){
UITouch * touch = [currentTouches objectAtIndex:i];
CGPoint point = [touch locationInView:self];
if(i==0){
max = point.x;
min = point.x;
} else{
if(point.x > max){
max = point.x;
}
else if(point.x < min) {
min = point.x;
}
}
}
self.leftLineX = min;
if([currentTouches count]==2)
self.rightLineX = max;
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray * array = [touches allObjects];
if([array count] == 1){
//NSLog(@"Single touch event ended!");
self.leftLineX = -1;
self.rightLineX = -1;
[currentTouches removeObject:[array objectAtIndex:0]];
//NSLog(@"currentTouches-: %d", [currentTouches count]);
[self setNeedsDisplay];
}
if([array count] == 2){
self.leftLineX = -1;
self.rightLineX = -1;
//NSLog(@"Multi touch event ended!");
for( int i = 0; i < [array count]; i++){
//NSLog(@"Touch %@", [array objectAtIndex:i]);
[currentTouches removeObject:[array objectAtIndex:i]];
}
[self setNeedsDisplay];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
//NSLog(@"Touch was cancelled!");
}
/*
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
NSLog(@"Draw Rect!");
}*/
- (void) dealloc{
[currentTouches release];
[super dealloc];
}
@end