在 Swift / iOS 动画期间获取动画高度

Getting animation height during animation in Swift / iOS

我正在尝试在我的应用程序中获取动画 UIView 的当前高度。

我使用以下方式为 UIView 设置动画:

function startFilling() {

    UIView.animateWithDuration(1.5, delay: 0, options: CurveEaseIn, animations: { () -> Void in
        self.filler.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
    }) { (success) -> Void in
        if success {
            self.removeGestureRecognizer(self.tap)
            self.delegate?.showSuccessLabel!()
        } else if !success {
            print("Not successful")
        }
    }
}

此动画是使用 UILongPressGestureRecognizer.Begin 状态触发的,如下所示:

if press.state == UIGestureRecognizerState.Began {
    startFilling()
} else if press.state == UIGestureRecognizerState.Ended {
    endFilling()
}

如果用户停止按下,那么函数 endFilling 被调用,我需要在他们停止按下时获取 self.filler 的当前高度。

麻烦的是高度总是returns动画中预期的结束高度(例如self.frame.height),即使按下只持续了持续时间的一部分。

我怎样才能达到这个高度?

核心动画并不是这样工作的。一旦你在 UIView.animateWithDuration 块中设置了一个动画值,无论动画是否完成,该值都会被设置。因此,当您在调用 startFilling 后请求 self.filler.frame 时,无论是在调用 startFilling 后 0.5 秒还是 5.0 秒,它都等于 CGRectMake(0, 0, self.frame.width, self.frame.height)。如果你想获得屏幕上的帧的当前值,你需要说 self.filler.layer.presentationLayer.frame。这应该用作只读值。如果你想中断动画并让它停在这个值,你应该这样说:

UIView.animateWithDuration(1.5, delay: 0, options: BeginFromCurrentState, animations: {
        self.filler.frame = self.layer.filler.presentationLayer.frame
    })

在 swift4 中

self.filler.layer.presentation().bounds