How to get the position of oldest object made during animation?

53 Views Asked by At

I make every second a UIImageView of a french frie called FrenchFrie. I also let them move along the screen with an animation.

-(void)FrenchFrieRain{
    FrenchFrie = [[UIImageView alloc] initWithFrame:CGRectMake(randomX,0,30,30)];
    FrenchFrie.image = [UIImage imageNamed:@"FrenchFriesMCDonalds"];
    FrenchFrie.userInteractionEnabled = YES;
    [self.view addSubview:FrenchFrie];
    [FrenchFrieArray addObject: FrenchFrie];
    [self letFrenchFrieFall];
}

-(void)letFrenchFrieFall {
    [UIView animateWithDuration:10.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        FrenchFrie.frame = CGRectMake(randomX, (int)[[UIScreen mainScreen] bounds].size.height + 40, FrenchFrie.frame.size.width, FrenchFrie.frame.size.height);
    } completion:^(BOOL finished){
        [[FrenchFrieArray objectAtIndex:0] removeFromSuperview];
}];

}

I need the position of the frie during the animation. I do this with the following code:

FrenchFrieFrame = [[FrenchFrie.layer presentationLayer] frame];

From NSLogging I know that with this code I get the position of the newest frie but I need the position of the oldest one. How can I get the position of the oldest French Frie? Any help would be really appreciated!

1

There are 1 best solutions below

4
sergio On

You are keeping track of the order in which you create FrenchFries in your FrenchFrieArray. So, the item at index 0 will be the oldest FrenchFrie, always. You could use:

FrenchFrieFrame = [[FrenchFrieArray[0].layer presentationLayer] frame];

For this to work, you should also remove the FrenchFrie from the array, besides removing it from the view:

    [[FrenchFrieArray objectAtIndex:0] removeFromSuperview];
    [[FrenchFrieArray removeObjectAtIndex:0];