0

我有:

UITouch *touch = [touches anyObject];

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

我想做的是if在两个触摸分开的语句中询问它。

4

2 回答 2

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));
}
于 2009-12-29T18:12:14.133 回答
-1

Touches 已经是一个数组。无需将它们复制到另一个数组中——只需使用 [touches objectAtIndex:n] 即可访问 touch n。

于 2010-05-04T12:39:11.073 回答