Lorenzo B
Lorenzo B

Reputation: 33428

Recipes on how to animate different UIViews at the same time

I have different UIViews that should be animated at the same time. My actual approach is the following:

- (void)moveRectangles {

    NSUInteger count = [self.dataSource numberOfRectangles];

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

where snapToPositionWithIndex: has been implemented like the following.

- (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];
}

As you can see, for each rectangle I create an animation block. Is this the right approach to follow or is there a more easier and performant way to achieve this? What if I have a lot of views to move?

Upvotes: 0

Views: 70

Answers (2)

Duncan C
Duncan C

Reputation: 131398

Your approach is fine. You could also write it with a single animation block where the for loop is inside the block, but I don't think that approach is any better than yours. The system creates a separate Core Animation for each object that's being animated regardless.

Upvotes: 2

Mohd Prophet
Mohd Prophet

Reputation: 1521

I think you are doing well with the above code,only thing i would like to replace is:-

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

with fast enumeration.

Upvotes: 2

Related Questions