我应该在主视图中管理两个不同的视图;我想在我的视图中检测手指的确切数量,也就是说如果我在“第一个视图”中有四个手指,我想要一个var,我说值为4,如果我在“第二个视图”中有3个手指,我想要另一个我告诉你一个值= 3.我告诉你我的代码不能正常工作。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSLog(@"cgpoint:%f,%f", touchLocation.x, touchLocation.y);
if (CGRectContainsPoint(view1.frame, touchLocation)){
NSLog(@"TOUCH VIEW 1");
totalTouch1 = [[event allTouches]count];
NSLog(@"TOTAL TOUCH 1:%d", totalTouch1);
}
else if (CGRectContainsPoint(view2.frame, touchLocation)){
NSLog(@"TOUCH VIEW 2");
totalTouch2 = [[event allTouches]count];
NSLog(@"TOTAL TOUCH 2:%d", totalTouch2);
}
}
如果我开始将手指放在“第一个视图”中,我的代码就可以了,但是例如,如果我将第四个手指放在第二个视图中,我的代码首先输入“if”并说我的手指还没有在第一个视图中。我不明白这个问题。
答案 0 :(得分:0)
[[event allTouches] anyObject];
只提取其中一个触摸。
你需要迭代所有的触摸:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int touch1 = 0;
int touch2 = 0;
NSArray *touches = [[event allTouches] allObjects];
for( UITouch *touch in touches ) {
CGPoint touchLocation = [touch locationInView:self.view];
NSLog(@"cgpoint:%f,%f", touchLocation.x, touchLocation.y);
if (CGRectContainsPoint(view1.frame, touchLocation)){
NSLog(@"TOUCH VIEW 1");
touch1 += 1;
NSLog(@"TOTAL TOUCH 1:%d", totalTouch1);
}
else if (CGRectContainsPoint(view2.frame, touchLocation)){
NSLog(@"TOUCH VIEW 2");
touch2 += 1;
NSLog(@"TOTAL TOUCH 2:%d", totalTouch2);
}
}
NSLog(@"Total touches on view 1: %d", touch1);
NSLog(@"Total touches on view 2: %d", touch2);
}