【发布时间】:2014-03-11 14:49:00
【问题描述】:
注意:我将ReactiveCocoaLayout 用于基于信号的动画。
我有一个 UILabel,我想将它绑定到视图模型上的 NSString* 属性。
RACSignal* statusSignal = [RACObserve(self, viewModel.status) distinctUntilChanged];
足够简单。但是,现在我想添加一些精美的动画。当status 发生变化时,我想连续发生什么:
- 淡出标签(
alpha从 1 -> 0) - 将新文本应用到 UILabel
- 将标签淡入(
alphafrom 1 -> 0)
这是我目前所能想到的:
RACSignal* statusSignal = [RACObserve(self, viewModel.status) distinctUntilChanged];
// An animation signal that initially moves from (current) -> 1 and moves from (current) -> 0 -> 1 after that
RACSignal* alphaValues = [[statusSignal flattenMap:^RACStream *(id _) {
// An animation signal that moves towards a value of 1
return [[[RACSignal return:@1]
delay:animationDuration]
animateWithDuration:animationDuration];
}] takeUntilReplacement:[[statusSignal skip:1] flattenMap:^RACStream *(id _) {
// An animation signal that moves towards a value of 0, waits for that to complete, then moves towards a value of 1
return [[[RACSignal return:@(0)]
animateWithDuration:animationDuration]
concat:[[[RACSignal return:@1]
delay:animationDuration]
animateWithDuration:animationDuration]];
}]];
RAC(self, statusLabel.alpha) = alphaValues;
// The initial status should be applied immediately. Combined with the initial animation logic above, this will nicely fade in the first
// status. Subsequent status changes are delayed by [animationDuration] in order to allow the "fade" animation (alpha from 1 -> 0) to
// finish before the text is changed.
RAC(self, statusLabel.text) = [[statusSignal take:1]
concat:[[statusSignal delay:animationDuration]
deliverOn:[RACScheduler mainThreadScheduler]]];
这行得通,但我无法摆脱它有点……设计的感觉。大部分复杂性来自我的基本情况 - 初始文本应该只是淡入,随后的文本更改应该淡出然后淡入。
关于如何简化或优化的任何想法?
【问题讨论】:
标签: ios objective-c reactive-cocoa