0

要找到 2 个触摸点的坐标,然后找到 2 个点之间的距离,从而生成要旋转的中心,我正在使用以下代码。我不确定这是否是最好的方法。

- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event

<SOME CODE>


if([touches count] == 2){
    for(touch in touches){
        if(self.pt1Flag){
            self.pt2=[touch locationInView:self.superview];
            self.pt2Flag = YES;
            self.pt1Flag = NO;
        }
        else{
            self.pt1 = [touch locationInView:self.superview];
            self.pt1Flag = YES;
            self.pt2Flag = NO;
        }

    }
}

self.delta = [self lengthDifferencePoint:self.pt1 andPoint:self.pt2 equiDistantFromPoint:self.center];

<SOME CODE>

}

主要用于单指触摸。有正确的方法吗?我也明白有

 [touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
}];

有人可以解释一下这个块的用法来找到接触点的坐标或任何比我更好/更清洁的解决方案。

4

2 回答 2

2

在这种情况下, enumerateObjectsUsingBlock 对您没有帮助,您没有在对象中过滤或执行任何类型的过程。事实上,for-in 循环本身也枚举了对象。

但是如果您知道只有 2 个对象不需要遍历它们,您可以简单地:

    NSArray *touchesArray = [touches allObjects];
    self.pt1 = [touchesArray[0] locationInView:self.superview];
    self.pt2 = [touchesArray[1] locationInView:self.superview];
于 2013-04-26T00:25:17.293 回答
1
NSArray *touchesArray = [touches allObjects];
for (int i=0; i< [touchesArray count]; i++) {
    NSString *variable = [NSString stringWithFormat:@"pt%d",i];
    [self setValue:[touchesArray objectAtIndex:i] forKey:variable];
}
于 2013-04-26T00:41:55.063 回答