ios - Objective-C: block 中的动画

标签 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 sliderPumpX 位置并停止动画:

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

问题是,当我调用 stop 方法时,我想得到 sliderPump 的当前位置,但我得到的是 sliderPimp 的最终位置动画结束,即使动画还没有完成。我怎样才能得到框架的当前位置?谢谢!

最佳答案

你应该使用表现层。

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

关于ios - Objective-C: block 中的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28817384/

相关文章:

ios - 在模拟器内启动时,命令行创建并安装了 iOS .app 崩溃

ios - 架构 arm64 : "_OBJC_CLASS_$_InterstitialAdView", 的 undefined symbol 引用自:

ios - 启动react-native run-ios时Native-Module不能为Null

ios - 通过将 webView 存储在 Array/plist 中来进行标签式浏览?

ios - UICollectionViewCell 改变位置

objective-c - 使用选择器评估对象链

html - 缩放弄乱了 z-index

android - 如何为 Android Activity 背景设置动画

css - 将 CSS 叠加动画化为淡入淡出

ios - 将手势识别器添加到特定的 UITabBarController tabBarItem