【发布时间】:2016-08-09 23:31:32
【问题描述】:
如何利用ReactiveX 依次执行异步调用? 即,在第一个调用完成后执行第二个调用。
更具体地说,我在 iOS 中使用 RxSwift,我想要链接在一起的异步是 UIView 动画(而不是在第一个动画的 completion 块内调用第二个动画)。
我知道我还有其他选择,例如 Easy Animation,但我想利用 Rx,因为我已经将它用于流。
另外,一种解决方案是(对于 3 个链式动画):
_ = UIView.animate(duration: 0.2, animations: {
sender.transform = CGAffineTransformMakeScale(1.8, 1.8)
})
.flatMap({ _ in
return UIView.animate(duration: 0.2, animations: {
sender.transform = CGAffineTransformMakeScale(0.8, 0.8)
})
})
.flatMap({ _ in
return UIView.animate(duration: 0.2, animations: {
sender.transform = CGAffineTransformIdentity
})
})
.subscribeNext({ _ in })
但我正在寻找更优雅的东西,使用 Rx 的正确方法。
【问题讨论】:
-
你的代码示例怎么了?你的意思是它是伪代码吗?您在
Void上调用flatMap,而不是SequenceType或Observable。另外,它是animateWithDuration:animations:,而不是animate:animations:。我不确定您包含该代码的意图。 -
我创建了
animate:...方法来返回一个可观察的,所以不,不是伪代码
标签: ios swift asynchronous rx-swift reactivex