我试图在多点触控中找到两个触摸的坐标。此代码在“UITouch * touch2 = ...”行上抛出SIGABRT。谁能告诉我哪里出错?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *touchArray = [touches allObjects];
UITouch *touch1 = [touchArray objectAtIndex:0];
UITouch *touch2 = [touchArray objectAtIndex:1];
CGPoint firstTouch = [touch1 locationInView:self.view];
CGPoint secondTouch = [touch2 locationInView:self.view];
}
答案 0 :(得分:1)
如果您想获得双击事件,则需要检查每个UITouch对象的tapCount
的值,而不是touches
是否有两个对象。
for (UITouch *touch in touches) {
if (touch.tapCount==1) {
// do something
} else if (touch.tapCount==2) {
// do something else
}
}
答案 1 :(得分:1)
阵列中很可能只有一次触摸。在尝试检索该索引处的对象之前,应检查该数组是否包含索引:
NSUInteger count = [array count];
id obj = (count > 1)? [array objectAtIndex:1] : nil;
id obj2 = (count > 2)? [array objectAtIndex:2] : nil;