一段时间以来,我一直在尝试获得这些结果。我似乎无法弄清楚。有人知道怎么做吗?
我试图按照数组的开头到结尾将两个对象相互比较。
Tilo 的解决方案:
for (int i=1; i<[tempRightArray count]; i++) {
UIImageView* letterA = [tempRightArray objectAtIndex:i-1];
UIImageView* letterB = [tempRightArray objectAtIndex:i];
NSLog(@"LetterA: %@",letterA);
NSLog(@"LetterB: %@",letterB);
//Distance between right side of Touched piece and Left side of new piece == Touch on Right
CGPoint midPointRightSidePiece = CGPointMake(CGRectGetMaxX(letterA.frame), CGRectGetMidY(letterA.frame));
CGPoint midPointLeftSidepiece = CGPointMake(CGRectGetMinX(letterB.frame), CGRectGetMidY(letterB.frame));
CGFloat distance = DistanceBetweenTwoPoints(midPointLeftSidepiece, midPointRightSidePiece);
NSLog(@"Distance: %f",distance);
}
更新了 Pauls block 解决方案:
[tempRightArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 0) {
UIImageView *letterB = (UIImageView*)obj;
id obj2 = [tempRightArray objectAtIndex:--idx]; // idx is the index of obj again given to you by the block args
UIImageView *letterA = (UIImageView*)obj2;
NSLog(@"LetterA: %@",letterA);
NSLog(@"LetterB: %@",letterB);
//Distance between right side of Touched piece and Left side of new piece == Touch on Right
CGPoint midPointRightSidePiece = CGPointMake(CGRectGetMaxX(letterA.frame), CGRectGetMidY(letterA.frame));
CGPoint midPointLeftSidepiece = CGPointMake(CGRectGetMinX(letterB.frame), CGRectGetMidY(letterB.frame));
CGFloat distance = DistanceBetweenTwoPoints(midPointLeftSidepiece, midPointRightSidePiece);
NSLog(@"Distance: %f",distance);
}
}];
最佳答案
for (int i=1; i<[myArray count]; i++) {
id obj1 = [myArray objectAtIndex:i-1];
id obj2 = [myArray objectAtIndex:i];
[self compare:obj1 to:obj2];
}
关于objective-c - For 循环会按照此模式在索引 : 0, 1 然后 1,2 然后 2,3 然后 3,4 处给我对象,直到数组计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942029/