0

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!

MatisDS
  • 161
  • 6

1 Answers1

1

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];
sergio
  • 68,819
  • 11
  • 102
  • 123
  • The fries are falling down the screen and I want to know if the frie and a basket at the bottom of the screen collide. If they don't collide the last frie should fall off the screen and disappear otherwise it should disappear at the center of the basket. I can't make OldestFrenchFrie because after the oldest falls off the screen, the second one who was created becomes the oldest. – MatisDS Jun 08 '14 at 13:33
  • sorry, do a cast: `[((FrenchFrie*)FrenchFrieArray[0]).layer` or `[[[FrenchFrieArray[0] layer] ...` – sergio Jun 08 '14 at 14:09
  • FrenchFrieFrame = [[FrenchFrieArray[0].layer presentationLayer] frame]; gave an error. I tried OldestFrenchFrie = [FrenchFrieArray objectAtIndex:0]; FrenchFrieFrame = [[OldestFrenchFrie.layer presentationLayer] frame]; this didn't gave an error but if I NSLog the place of the frie I get x-co = 0 and Y-co = 0. – MatisDS Jun 08 '14 at 14:11
  • Also when I add FrenchFrie to the array the value of the array stays nil in the debug area. – MatisDS Jun 08 '14 at 14:15