I'm trying to change the visibility of a view based on the current content offset y value of a scrollview using Reactive Cocoa. My thought was to create an observer on an NSNumber property that would get changed in the scrollview delegate's scrollViewDidScroll. Here's the code that I've added to viewDidLoad:

RAC(self.headerView, hidden) = [RACObserve(self, offset) subscribeNext:^(NSNumber *value) {
    return @([value integerValue] > 0);
}];

Setting a breakpoint on the return statement, I see that this is called once, but never again. What am I missing?

有帮助吗?

解决方案

The solution you describe, implementing a method that calls -sendNext: on a subject, is so common that there's a method that does that all for you: -rac_signalForSelector:. In the case of delegates, you can call -rac_signalForSelector: on the delegate, like so:

RAC(self.headerView, hidden) = [[(id)scrollView.delegate
    rac_signalForSelector:@selector(scrollViewDidScroll:)]
    reduceEach:^(UIScrollView *scrollView) {
        return @(scrollView.contentOffset.y > 0);
    }];

One gotcha that comes up with delegates is that you must make sure you invoke any -rac_signalForSelector: calls before assigning the delegate.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top