So I'm doing custom gesture recognizing and I've ran into trouble. I need to monitor all the current touches, but touchesBegan
, touchesEnded
, touchesMoved
and touchesCancelled
all only give me the touches that they are monitoring. I need to do calculations on all the touches, regardless of wether they are being monitored or not. I've done some research and I've seen suggestions to monitor the touches myself, but it hasn't been going well for me. Here is my code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[myTouches unionSet:touches];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *objects = [myTouches allObjects];
for (UITouch *touch in touches) {
for (UITouch *touch2 in objects) {
if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
[myTouches removeObject:touch2];
[myTouches addObject:touch];
}
}
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[myTouches minusSet:touches];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[myTouches minusSet:touches];
NSArray *objects = [myTouches allObjects];
for (UITouch *touch in touches) {
for (UITouch *touch2 in objects) {
if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
[myTouches removeObject:touch2];
}
}
}
}
What I want: myTouches
should be an accurate representation of all the touches on the screen.
What I get: myTouches
constantly grows, although sometimes touches from events are correctly registered as the same as touches in myTouches
. I also did a test where I had an integer count the number of touches registered in touchesBegan
and subtracted the touches registered in touchesEnded
and the number always came out to be positive, meaning that more touches are being registered than ended. Please help!
Try subclass of UIApplication to monitor all the touches in application.
then use the event
sendEvent
inMyUIApplication
and don't forget to add the class in main