在通过 ReactiveSwift Signal Producer
向 View 的 alpha
属性发送一些值后,我尝试对 View 的 alpha
属性进行动画。
以下是我目前在没有动画的情况下的做法。
// Somewhere in View Model (for all code below)
let shouldShowShutter = MutableProperty<Bool>(false)
// In my View
self.shutterButton.reactive.alpha <~ self.viewModel.shouldShowShutter.map({ (show) -> CGFloat in
return show ? 1:0.0
})
我可以通过以下方式不优雅地为 View 设置动画:
self.viewModel.shouldShowShutter.producer.startWithSignal { (observer, disposable) in
observer.map({ (show) -> CGFloat in
return show ? 1:0.0
}).observeValues({ [unowned self] (alpha) in
UIView.animate(withDuration: 0.5, animations: {
self.shutterButton.alpha = alpha
})
})
}
但我应该能够使用 ReactiveAnimation
为 View 添加动画:
self.shutterButton.reactive.alpha <~ self.viewModel.shouldShowShutter.map({ (show) -> CGFloat in
return show ? 1:0.0
}).animateEach(duration: 0.2).join(.Concat)
问题:在我提出问题时,ReactiveCocoaLayout
和 ReactiveAnimation
似乎都不再工作了,至少在 Swift 3 或 ReactiveCocoa 5
,因为它们很大程度上依赖于遗留的 RACSignals
。
是否有更优雅的方式使用 ReactiveCocoa 5 将信号流动画化到 ReactiveCocoa 组件?
最佳答案
我以前不知道 ReactiveAnimation,但我真的很喜欢这种方法。
所以我将其更新为 RAC 5.0/Swift 3.2,看看 my fork 。我还将创建一个拉取请求,让我们看看是否能让项目恢复生机。
我包含了一个小型 iOS 演示项目来演示其用法:
label.reactive.center <~ SignalProducer.timer(interval: .seconds(1), on: QueueScheduler.main)
.map { _ in return self.randomPoint() }
// In order to demonstrate different flatten strategies,
// the animation duration is larger than the animation interval,
// thus a new animation begins before the running animation is finished
.animateEach(duration: 1.5, curve: .EaseInOut)
// With the .concat flatten strategy, each animations are concatenated.
// Each animation finisheds, before the next one starts.
// This also means, that animations are queued
.flatten(.concat)
// With the .merge flatten strategy, each animation is performed immediately
// If an animation is currently running, it is cancelled and the next animation starts from the current animation state
//.flatten(.merge)
关于ios - ReactiveCocoa 5 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525369/