2016-12-16 124 views
2

因此,当您在主屏幕上向下滚动并且模糊背景时,我使用新的UIViewPropertyAnimator和UIVisualEffectView实现与Spotlight搜索相同的功能。动画UIViewPropertyAnimator的fractionComplete模糊背景

我使用fractionComplete属性来设置在平移UIView时需要模糊多少。

animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { 
     self.blurEffectView.effect = nil 
    } 

并且模糊量被改变为0.0-1.0之间的值。

 animator?.fractionComplete = blurValue 

但是,当我取消移动手势我想模糊动画从那里是没有模糊回(例如〜 - > 1.0)与类似0.4毫秒的持续时间。

现在我只是在平移手势被取消时将fractionComplete设置为1.0。相反,我想为它制作动画。
我曾尝试UIView.animate(withDuration ..但它不影响UIViewPropertyAnimators fractionComplete,和那模糊的UIVisualEffectView的唯一途径。

任何想法?

+0

我有同样的错误,在这一点上,我认为它可能是一个Apple效能被清除的错误,而不是它可以在0和1之间转换的值。确保向苹果提交雷达。 – livings124

+0

只是为了确认,当您开始平移时,动画开始模糊背景。当你停止平移时,你希望它恢复到原始状态? –

回答

10

看来fractionComplete有错误(我的#2问题:UIViewPropertyAnimator does not update the view when expected),rdar://30856746酒店只设置从inactiveactive状态,但不更新视图,因为(我认为)有不触发另一内部状态

要。解决你可以做的问题是:

animator.startAnimation() // This will change the `state` from inactive to active 
animator.pauseAnimation() // This will change `isRunning` back to false, but the `state` will remain as active 

// Now any call of `fractionComplete` should update your view correctly! 
animator.fractionComplete = /* your value here */ 

这里是一个操场片断打转转:

let liveView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 50)) 
liveView.backgroundColor = .white 

PlaygroundPage.current.needsIndefiniteExecution = true 
PlaygroundPage.current.liveView = liveView 

let square = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) 
square.backgroundColor = .red 

liveView.addSubview(square) 

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

animator.addAnimations { 

    square.frame.origin.x = 350 
} 

let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .dark)) 
blurView.frame = liveView.bounds 

liveView.addSubview(blurView) 

animator.addAnimations { 

    blurView.effect = nil 
} 

// If you want to restore the blur after it was animated, you have to 
// safe a reference to the effect which is manipulated 
let effect = blurView.effect 

animator.addCompletion { 
    // In case you want to restore the blur effect 
    if $0 == .start { blurView.effect = effect } 
} 

animator.startAnimation() 
animator.pauseAnimation() 

DispatchQueue.main.asyncAfter(deadline: .now() + 2) { 

    animator.fractionComplete = 0.5 
} 

DispatchQueue.main.asyncAfter(deadline: .now() + 4) { 

    // decide the direction you want your animation to go. 
    // animator.isReversed = true 
    animator.startAnimation() 
} 
+0

工程就像一个魅力!希望不久的将来会有解决办法。 – alengqvist

+0

谢谢!杜绝了错误。当你没有执行'startAnimation()'''''''''''''''动画()'舞蹈时,似乎还有更多的问题。调用'continueAnimation()'不会有小的延迟。 – Klaas

+0

@Klaas现在我甚至不确定它是真正的bug还是有意的。在WWDC17中,苹果工程师总是使用'pauseAnimation()',甚至不用调用'startAnimation()',这似乎也可以做到这一点。 :/也可能是他们正在解决这个问题。 :D – DevAndArtist