Browse files

improve

  • Loading branch information...
1 parent 8bc5f84 commit dbc4a213c0d0b184dc8c6ed23150514acf677260 @masuidrive masuidrive committed Aug 5, 2010
Showing with 151 additions and 6 deletions.
  1. +145 −4 Classes/MultitouchModule.m
  2. +6 −2 app.js
View
149 Classes/MultitouchModule.m
@@ -84,9 +84,9 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableDictionary *evt = [NSMutableDictionary dictionaryWithDictionary:[TiUtils pointToDictionary:[touch locationInView:self]]];
[evt setValue:[TiUtils pointToDictionary:[touch locationInView:nil]] forKey:@"globalPoint"];
- NSMutableArray *ts = [NSMutableArray arrayWithCapacity:10];
+ NSMutableDictionary *ts = [NSMutableDictionary dictionary];
for (UITouch* t in touches) {
- [ts addObject:[TiUtils pointToDictionary:[t locationInView:nil]]];
+ [ts setObject:[TiUtils pointToDictionary:[t locationInView:nil]] forKey:[NSString stringWithFormat:@"%p",t]];
}
[evt setValue:ts forKey:@"points"];
@@ -123,9 +123,9 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableDictionary *evt = [NSMutableDictionary dictionaryWithDictionary:[TiUtils pointToDictionary:[touch locationInView:self]]];
[evt setValue:[TiUtils pointToDictionary:[touch locationInView:nil]] forKey:@"globalPoint"];
- NSMutableArray *ts = [NSMutableArray arrayWithCapacity:10];
+ NSMutableDictionary *ts = [NSMutableDictionary dictionary];
for (UITouch* t in touches) {
- [ts addObject:[TiUtils pointToDictionary:[t locationInView:nil]]];
+ [ts setObject:[TiUtils pointToDictionary:[t locationInView:nil]] forKey:[NSString stringWithFormat:@"%p",t]];
}
[evt setValue:ts forKey:@"points"];
if ([proxy _hasListeners:@"touchmove"])
@@ -158,4 +158,145 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
}
}
+
+- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ if (handlesTaps)
+ {
+ BOOL allTouchesEnded = ([touches count] == [[event touchesForView:self] count]);
+
+ // first check for plain single/double tap, which is only possible if we haven't seen multiple touches
+ if (!multipleTouches)
+ {
+ UITouch *touch = [touches anyObject];
+ tapLocation = [touch locationInView:self];
+
+ if ([touch tapCount] == 1)
+ {
+ [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:DOUBLE_TAP_DELAY];
+ }
+ else if([touch tapCount] == 2)
+ {
+ [self handleDoubleTap];
+ }
+ }
+
+ // check for 2-finger tap if we've seen multiple touches and haven't yet ruled out that possibility
+ else if (multipleTouches && twoFingerTapIsPossible)
+ {
+
+ // case 1: this is the end of both touches at once
+ if ([touches count] == 2 && allTouchesEnded)
+ {
+ int i = 0;
+ int tapCounts[2] = {0,0};
+ CGPoint tapLocations[2];
+ for (UITouch *touch in touches) {
+ tapCounts[i] = [touch tapCount];
+ tapLocations[i] = [touch locationInView:self];
+ i++;
+ }
+ if (tapCounts[0] == 1 && tapCounts[1] == 1)
+ {
+ // it's a two-finger tap if they're both single taps
+ tapLocation = midpointBetweenPoints(tapLocations[0], tapLocations[1]);
+ [self handleTwoFingerTap];
+ }
+ }
+
+ // case 2: this is the end of one touch, and the other hasn't ended yet
+ else if ([touches count] == 1 && !allTouchesEnded)
+ {
+ UITouch *touch = [touches anyObject];
+ if ([touch tapCount] == 1)
+ {
+ // if touch is a single tap, store its location so we can average it with the second touch location
+ tapLocation = [touch locationInView:self];
+ }
+ else
+ {
+ twoFingerTapIsPossible = NO;
+ }
+ }
+
+ // case 3: this is the end of the second of the two touches
+ else if ([touches count] == 1 && allTouchesEnded)
+ {
+ UITouch *touch = [touches anyObject];
+ if ([touch tapCount] == 1)
+ {
+ // if the last touch up is a single tap, this was a 2-finger tap
+ tapLocation = midpointBetweenPoints(tapLocation, [touch locationInView:self]);
+ //[self handleTwoFingerTap];
+ }
+ }
+ }
+
+ // if all touches are up, reset touch monitoring state
+ if (allTouchesEnded)
+ {
+ twoFingerTapIsPossible = YES;
+ multipleTouches = NO;
+ }
+ }
+
+ if (handlesTouches)
+ {
+ UITouch *touch = [touches anyObject];
+ NSMutableDictionary *evt = [NSMutableDictionary dictionaryWithDictionary:[TiUtils pointToDictionary:[touch locationInView:self]]];
+ [evt setValue:[TiUtils pointToDictionary:[touch locationInView:nil]] forKey:@"globalPoint"];
+ NSMutableDictionary *ts = [NSMutableDictionary dictionary];
+ for (UITouch* t in touches) {
+ [ts setObject:[TiUtils pointToDictionary:[t locationInView:nil]] forKey:[NSString stringWithFormat:@"%p",t]];
+ }
+ [evt setValue:ts forKey:@"points"];
+ if ([proxy _hasListeners:@"touchend"])
+ {
+ [proxy fireEvent:@"touchend" withObject:evt propagate:YES];
+ [self handleControlEvents:UIControlEventTouchCancel];
+ }
+ }
+ if (handlesSwipes)
+ {
+ touchLocation = CGPointZero;
+ }
+
+ if (touchDelegate!=nil)
+ {
+ [touchDelegate touchesEnded:touches withEvent:event];
+ }
+}
+
+- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ if (handlesTaps)
+ {
+ twoFingerTapIsPossible = YES;
+ multipleTouches = NO;
+ }
+ if (handlesTouches)
+ {
+ UITouch *touch = [touches anyObject];
+ CGPoint point = [touch locationInView:self];
+ NSDictionary *evt = [TiUtils pointToDictionary:point];
+ NSMutableDictionary *ts = [NSMutableDictionary dictionary];
+ for (UITouch* t in touches) {
+ [ts setObject:[TiUtils pointToDictionary:[t locationInView:nil]] forKey:[NSString stringWithFormat:@"%p",t]];
+ }
+ [evt setValue:ts forKey:@"points"];
+ if ([proxy _hasListeners:@"touchcancel"])
+ {
+ [proxy fireEvent:@"touchcancel" withObject:evt propagate:YES];
+ }
+ }
+ if (handlesSwipes)
+ {
+ touchLocation = CGPointZero;
+ }
+
+ if (touchDelegate!=nil)
+ {
+ [touchDelegate touchesCancelled:touches withEvent:event];
+ }
+}
@end
View
8 app.js
@@ -50,13 +50,17 @@ window.addEventListener('touchmove', function(ev) {
window.addEventListener('touchend', function(ev) {
Ti.API.info(ev);
- // can't handle multi touch in 'touchend' and 'touchcancel' event.
});
+window.addEventListener('touchcancel', function(ev) {
+ Ti.API.info(ev);
+});
+
+
window.addEventListener('singletap', function(ev) {
// DON'T REMOVE THIS LISTENER!!
// hack for multi touch module
});
-window.open();
+window.open();

0 comments on commit dbc4a21

Please sign in to comment.