如何在触摸开始事件中仅访问我的一个触摸

时间:2009-12-29 18:07:16

标签: iphone uitouch multi-touch

我有:

UITouch *touch = [touches anyObject];

    if ([touches count] == 2) {
        //preforming actions                                                             
    }

我想做什么在if声明中询问两个接触是否是分开的。

2 个答案:

答案 0 :(得分:1)

您可以迭代搜索:

if([touches count] == 2) {
    for(UITouch *aTouch in touches) {
        // Do something with each individual touch (e.g. find its location)
    }
}

编辑:如果您想要找到两个触摸之间的距离,并且您知道有两个触摸,您可以单独抓取每个触摸然后进行一些数学运算。例如:

float distance;
if([touches count] == 2) {
    // Order touches so they're accessible separately
    NSMutableArray *touchesArray = [[[NSMutableArray alloc] 
                                     initWithCapacity:2] autorelease];
    for(UITouch *aTouch in touches) {
        [touchesArray addObject:aTouch];
    }
    UITouch *firstTouch = [touchesArray objectAtIndex:0];
    UITouch *secondTouch = [touchesArray objectAtIndex:1];

    // Do math
    CGPoint firstPoint = [firstTouch locationInView:[firstTouch view]];
    CGPoint secondPoint = [secondTouch locationInView:[secondTouch view]];
    distance = sqrtf((firstPoint.x - secondPoint.x) * 
                     (firstPoint.x - secondPoint.x) + 
                     (firstPoint.y - secondPoint.y) * 
                     (firstPoint.y - secondPoint.y));
}

答案 1 :(得分:-1)

触摸已经是一个数组。没有必要将它们复制到另一个数组 - 只需使用[touches objectAtIndex:n]来访问touch n。