在我的应用中,我需要捕捉屏幕上手指的确切数量,我尝试了两种方法,但我遇到了 2 个不同的问题。
第一种方式:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
int count = [allTouches count];
NSLog(@"number of touch:%d", count);
}
如果我同时使用更多手指,这会给我一个不准确的触摸次数。
第二种方式:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
NSLog(@"number of touch:%d", totalTouch);
}
通过这种方式,我使用了一个全局变量 (totalTouch),每次调用 touchbegan 时我都会增加它,并且我有完美的触摸次数。 当然,我在触摸端中将此变量设置为“0”
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
totalTouch = 0;
}
我的问题是,我在 touchbegan 中进行控制的第二种方式是:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
if (totalTouch == numberToVerify){
//IT ENTER HERE EVERYTIME INSIDE THIS IF
}
else{
}
}
所以每次它进入 if-conditions 时,我不想要它,我只想在我有最终的触摸次数时才做这个控制......
最佳答案
在你的
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
int num = [touches count];
totalTouch = totalTouch+num;
NSLog(@"number of touch:%d", totalTouch);
}
您可以通过以下方式获取屏幕上的手指数量
[[event allTouches]count]
这可以从 - (void)touchesBegan:
- (void)touchesEnded:
或 - (void)touchesMoved:
访问p>
关于IOS:正确的屏幕触摸次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15885728/