关于如何同时为不同的UIViews制作动画的食谱

时间:2015-02-12 11:46:17

标签: ios objective-c performance uikit uiviewanimation

我有不同的UIView s应该同时进行动画处理。我的实际方法如下:

- (void)moveRectangles {

    NSUInteger count = [self.dataSource numberOfRectangles];

    for (NSUInteger i = 0; i < count; i++) {
        [self snapToPositionWithIndex:i];
    }
}

其中snapToPositionWithIndex:已实施如下。

- (void)snapToPositionWithIndex:(NSUInteger)index {

    CGPoint point = [[self.positions objectAtIndex:index] CGPointValue];
    UIView *rectangleView = [self.items objectAtIndex:index];

    [UIView animateWithDuration:self.animationTimeInterval animations:^{
        rectangleView.center = point;

    } completion:nil];
}

如您所见,我为每个矩形创建了一个动画块。这是正确的方法,还是有更简单,更高效的方法来实现这一目标?如果我有很多观点要移动怎么办?

2 个答案:

答案 0 :(得分:2)

我认为你对上面的代码做得很好,我想要替换的是: -

for (NSUInteger i = 0; i < count; i++)

快速枚举。

答案 1 :(得分:2)

你的方法很好。你也可以用一个动画块来编写它,其中for循环在块内,但我不认为这种方法比你的更好。系统为每个被动画的对象创建单独的核心动画。