我正在尝试在通过ReactiveSwift Signal Producer
发送一些值后对视图的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
})
我可以通过以下方式不优雅地对视图进行动画处理:
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
对视图进行动画处理:
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组件?
我以前不知道反应动画,但我真的很喜欢这种方法。
所以我为 RAC 5.0/Swift 3.2 更新了它,看看我的分叉。我还将创建一个拉取请求,让我们看看我们是否可以让项目恢复生机。
我包含一个小型的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)