无法使用UIViewPropertyAnimator.fractionComplete驱动UIView.transition

时间:2018-08-22 10:47:05

标签: ios swift uiviewpropertyanimator

我正在尝试使用滑块驱动具有2个图像值的UIImageView的过渡(在此示例中) 但是在UIView.transition中使用UIViewPropertyAnimator(应该可以) 没有。我想念什么吗?

let animator = UIViewPropertyAnimator(duration: 3.0, curve: .linear)

let crossDissolve = {
  self.imageView.image = UIImage(named: "deadpool.jpg")
  UIView.transition(
    with: self.imageView,
    duration: 4.0,
    options: .transitionCrossDissolve,
    animations: {},
    completion: nil
  )
}

animator.addAnimations(crossDissolve)

这里是一个操场,上面有这个问题的例子, 只需将滑块从一侧拖到另一侧,图像应该会交叉柔和地溶解

类似的东西:

img

游乐场:https://www.dropbox.com/s/ix8nfxg43ij68q3/UIViewPropertyAnimator.playground.zip?dl=0

1 个答案:

答案 0 :(得分:1)

在提供的代码中,您没有在动画闭包中定义任何动画,我对这个API不太熟悉,但是我想您会想要类似的东西。(我尚未测试此代码)

let animator = UIViewPropertyAnimator(duration: 3.0, curve: .linear)

let crossDissolve = {
  UIView.transition(
    with: self.imageView,
    duration: 4.0,
    options: .transitionCrossDissolve,
    animations: {
        self.imageView.image = UIImage(named: "deadpool.jpg")
    },
    completion: nil
  )
}

animator.addAnimations(crossDissolve)

然后可以使用在滑块.valueChanged事件上调用的函数来处理动画进度

slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged) 

func sliderValueChanged(sender: UISlider) {
    myAnimation.fractionComplete = CGFloat(sender.value)
}