Objective-C:块中的动画

时间:2015-03-02 18:56:35

标签: ios objective-c animation objective-c-blocks

我有一个UIImageView sliderPump,我通过一个接一个地调用两个方法无限次地从屏幕的右侧移动到另一侧:

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

}

我使用此代码找出UIImageView sliderPump X 位置并停止动画:

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

问题在于,当我调用stop方法时,我想要得到sliderPump的当前位置,但我得到的是动画结束时sliderPimp的最终位置,即使动画尚未完成。如何获得框架的当前位置?谢谢!

1 个答案:

答案 0 :(得分:2)

您应该使用表示层。

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