objective-c - For 循环会按照此模式在索引 : 0, 1 然后 1,2 然后 2,3 然后 3,4 处给我对象,直到数组计数?

标签 objective-c ios for-loop

一段时间以来,我一直在尝试获得这些结果。我似乎无法弄清楚。有人知道怎么做吗?

我试图按照数组的开头到结尾将两个对象相互比较。

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/

相关文章:

javascript - 使用 JavaScript 循环查找数组中的最大数字

ios - 如何在 drawRect 中获取 UIView 大小

ios - 在搜索栏上键入会破坏布局

ios - 获取 UIWebView 网址! (myWebView.request.URL.absoluteString 不工作)

ios - 如何根据具有自动调整大小的传入数据在 Collection View 单元格中添加随机内容(UIKit 元素)?

ios - Swift 过滤数组元素

javascript - 反转 JavaScript 中的字符串 : Why is my answer not passing the test?

iOS 有边框按钮点击时的行为

ios - 如何仅显示在 Objective C 的 TableView 中具有内容的表行

ios - 你如何使用 iPhone 可达性通知