SmartTree
SmartTree

Reputation: 1471

Objective-C: Animations in Blocks

I have a UIImageView sliderPump which I move from right to left side of the screen infinite times by calling two methods one after another:

    -(void)pumpGoesRight
    {
        if (slide)
        {
            [UIView animateWithDuration:0.8 animations:^
             {

                 [sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x+235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];

             } completion:^(BOOL finished)
             {
                 [self pumpGoesLeft];
             }];
        }
        else
            return;
    }
    -(void)pumpGoesLeft
    {
        if (slide)
        {

            [UIView animateWithDuration:0.8 animations:^
             {
                 [sliderPump setFrame:CGRectMake(sliderPump.frame.origin.x-235, sliderPump.frame.origin.y, sliderPump.frame.size.width, sliderPump.frame.size.height)];

             } completion:^(BOOL finished)
             {
                 [self pumpGoesRight];
             }];

            [self performSelector:@selector(pumpGoesRight) withObject:nil afterDelay:0.8];
        }
        else
            return;

}

And I use this code to find out the X position of the UIImageView sliderPump and stop the animation:

-(void)stop
{
    slide = NO;
    NSLog(@"Slide Pump position: %f", sliderPump.layer.frame.origin.x);
    [self.view.layer removeAllAnimations];   
}

The problem is that when I call stop method, I suppose to get a current position of the sliderPump, but what I get is the final position of the sliderPimp at the end of the animation, even if the animation is not completed yet. How can I get the current position of the frame? Thanks!

Upvotes: 3

Views: 402

Answers (1)

BorisE
BorisE

Reputation: 385

You should use the presentation layer.

NSLog(@"Slide Pump position: %f", [sliderPump.layer.presentationLayer frame].origin.x);

Upvotes: 2

Related Questions