UIViewPropertyAnimator

UIViewPropertyAnimator

本文介绍了有没有一种方法可以观察UIViewPropertyAnimator中fractionComplete的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究iOS 10中非常酷的全新 UIViewPropertyAnimator 类,它使您可以轻松执行诸如暂停,恢复,并反转飞行中的UIView动画。过去,您必须操纵系统创建的基础CAAnimations才能使用UIView动画。

I've been looking at the very cool new UIViewPropertyAnimator class in iOS 10. It lets you easily do things like pause, resume, and reverse in-flight UIView animations. It used to be that you had to manipulate the underlying CAAnimations the system created in order to do that with UIView animations.

新的 UIViewPropertyAnimator 类的属性为 fractionComplete 。它的范围从0.0(动画开始)到1.0(动画结束)。如果更改该值,则可以从头到尾擦洗动画。

The new UIViewPropertyAnimator class has a property fractionComplete. It varies from 0.0 (beginning of animation) to 1.0 (end of animation.) If you change the value, you can "scrub" an animation from beginning to end.

我在Github上有一个名为的演示项目,该项目可让您使用滑块使用相当神秘的CAAnimation技巧来回擦洗UIView和CAAnimations。在动画运行时,滑块从头到尾移动以显示动画的进度。该项目是在Swift发行之前用Objective-C编写的,但是其基本技术在Objective-C或Swift中都是相同的。

I have a demo project on Github called KeyframeViewAnimations that lets you use a slider to scrub UIView and CAAnimations back and forth using fairly arcane CAAnimation tricks. As the animation runs, the slider moves from beginning to end to show the progress of the animation. That project was written in Objective-C before Swift was released, but the underlying techniques are the same in Objective-C or Swift.

我决定使用以下方法创建一个等效项目 UIViewPropertyAnimator 。有一些怪癖,但这很简单。我无法弄清楚如何做的一件事是观察动画的 fractionComplete 属性的进度,以便可以在动画进行时更新滑块。我尝试在 fractionComplete 属性上设置KVO(键值观察),但是在动画完成之前它不会触发KVO通知。

I decided to create an equivalent project using UIViewPropertyAnimator. There are some quirks, but it's fairly straightforward. One thing I could not figure out how to do cleanly was to observe the progress of the animation's fractionComplete property so that I could update the slider as the animation progresses. I tried setting up KVO (key-value-observing) on the fractionComplete property, but it doesn't trigger a KVO notification until the animation is complete.

我最终要做的是设置一个运行时间非常短的计时器,并查询 UIViewPropertyAnimator fractionComplete 属性,并不断更新滑块。它可以工作,但不是一种很干净的处理方式。如果可以通过某种方式获得有关动画进度的通知,那就更好了。

What I ended up doing is setting up a timer that runs on a very small interval and queries the UIViewPropertyAnimator's fractionComplete property repeatedly and updating the slider as it progresses. It works, but it's not a very clean way of doing things. It would be much better if there was a way to get notified about the progress of the animation.

我希望我缺少一些东西。

I'm hoping there's something I'm missing.

您可以通过以下链接在Github上查看基于 UIViewPropertyAnimator 的项目:。

You can see the UIViewPropertyAnimator based project on Github at this link: UIViewPropertyAnimator-test.

从中获取进度值的代码动画师是这样的代码:

The code that gets the progress value from the animator is this code:

func startTimer(_ start: Bool) {
  if start {
    timer = Timer.scheduledTimer(withTimeInterval: 0.02,
     repeats: true) {
        timer in
        var value = Float(self.viewAnimator.fractionComplete)
        if self.animatingBackwards {
          value = 1 - value
        }
        self.animationSlider.value = value
     }
  }
  else {
    self.timer?.invalidate()
  }
}

我应该可以将上面的代码转换为使用 CADisplayLink 计时器而不是普通计时器(NSTimer),但是我希望有更好的方法。

I should probably convert the code above to use a CADisplayLink timer instead of a vanilla Timer (NSTimer) but I'm hoping there's a better way.

推荐答案

您是否尝试将UIViewPropertyAnimator子类化,然后覆盖fractionComplete属性?

Did you try subclassing UIViewPropertyAnimator and then overriding the fractionComplete property?

class MyPropertyAnimator : UIViewPropertyAnimator {
    override var fractionComplete: CGFloat {
        didSet {
            print("didSetFraction")
        }
    }
}

这篇关于有没有一种方法可以观察UIViewPropertyAnimator中fractionComplete的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 08:45