我需要检测两个手指触摸事件。如果我同时用两根手指触摸屏幕,那么一切都可以。只需使用这样的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint point1 = [touch locationInView:self];
CGPoint point2 ;
NSLog(@"First: %f %f", point1.x, point1.y) ;
if ([[touches allObjects] count] > 1) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
point2 = [touch2 locationInView:self];
NSLog(@"Second: %f %f", point2.x, point2.y) ;
}
}
但是,如果我按住一根手指,然后用另一根手指触摸屏幕,则此代码不起作用。如何实施?很难做吗?
确保 UIView 有multipleTouchEnabled=YES
,默认为NO
.
编辑:我明白问题是什么了。在touchesBegan:withEvent:中,你只会得到新的触摸。您无法获得所有主动接触。如果可能的话,您不太可能同时开始多次触摸。要检查是否有多个活动触摸 int TouchBegan:请尝试以下操作:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForView:self] count] > 1) {
NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ;
}
[super touchesBegan:touches withEvent:event] ;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)