同时移动4个NSMutableArray对象

时间:2011-04-20 16:59:46

标签: iphone arrays image

我用图像视图制作了一个正方形。它们中的每一个都是Object的{​​{1}}。 如果我触摸左上角,我希望我的图像方块的中间位于左下角。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我不确定你的问题是什么。我将假设以下内容:

  1. 您的网格是一个正方形,其维度存储在gridWidth

  2. 网格中的对象存储在从左上角到右下角排序的gridArray

  3. 当在网格中的对象的左上角检测到点按时,会调用userTappedObject:

  4. 你有一个名为`layoutGrid'的函数,用于遍历数组并设置每个对象的框架

  5. 示例:

    - (void)userTappedObject:(id)tappedObject
    {
        NSUInteger indexOfBottomLeft = gridWidth * (gridWidth - 1);
    
        // to exchange the tapped object with the object in the bottom left corner
        NSUInteger indexOfTappedObject = [myMutableArray indexOfObject:tappedObject];
        [gridArray exchangeObjectAtIndex:indexOfTappedObject withObjectAtIndex:indexOfBottomLeft];
    
        // to move the tapped object to the bottom left corner, collapsing objects to the left
        // from the bottom left corner to make room
        [gridArray removeObject:tappedObject];
        [gridArray insertObject:tappedObject atIndex:indexOfBottomLeft];
    
        // layout the grid (this could animate changes if desired)
        [self layoutGrid];
    }