多点触控协调问题iPhone

时间:2011-06-09 21:25:10

标签: iphone cocoa-touch multi-touch

我试图在多点触控中找到两个触摸的坐标。此代码在“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];
}

2 个答案:

答案 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;