Skip to content

Instantly share code, notes, and snippets.

@kittinunf
Created November 14, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kittinunf/e6b20b4e6ab8b2c4e91b to your computer and use it in GitHub Desktop.
Save kittinunf/e6b20b4e6ab8b2c4e91b to your computer and use it in GitHub Desktop.
//
// UIScrollView+RACSupport.h
// Taskworld
//
// Created by Kittinun Vantasin on 8/5/15.
// Copyright (c) 2015 Taskworld. All rights reserved.
//
@import UIKit;
@class RACDelegateProxy;
@class RACSignal;
@interface UIScrollView (RACSupport)
@property (nonatomic, strong, readonly) RACDelegateProxy *rac_delegateProxy;
- (RACSignal *)rac_didScroll;
- (RACSignal *)rac_didScrollHorizontally;
- (RACSignal *)rac_didScrollVertically;
@end
//
// UIScrollView+RACSupport.m
// Taskworld
//
// Created by Kittinun Vantasin on 8/5/15.
// Copyright (c) 2015 Taskworld. All rights reserved.
//
@import ReactiveCocoa;
@import ObjectiveC;
#import "UIScrollView+RACSupport.h"
@implementation UIScrollView (RACSupport)
static void RACUseDelegateProxy(UIScrollView *self) {
if (self.delegate == self.rac_delegateProxy) return;
self.rac_delegateProxy.rac_proxiedDelegate = self.delegate;
self.delegate = (id)self.rac_delegateProxy;
}
- (RACDelegateProxy *)rac_delegateProxy {
RACDelegateProxy *proxy = objc_getAssociatedObject(self, _cmd);
if (!proxy) {
proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UIScrollViewDelegate)];
objc_setAssociatedObject(self, _cmd, proxy,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return proxy;
}
- (RACSignal *)rac_didScroll {
RACSignal *signal = [[[[self.rac_delegateProxy signalForSelector:@selector(scrollViewDidScroll:)]
mapReplace:self] takeUntil:self.rac_willDeallocSignal]
setNameWithFormat:@"%@ -rac_didScroll",
self.rac_description];
RACUseDelegateProxy(self);
return signal;
}
- (RACSignal *)rac_didScrollHorizontally {
RACSignal *signal = [[[[self.rac_delegateProxy signalForSelector:@selector(scrollViewDidScroll:)]
reduceEach:^RACTuple *(UIScrollView *scrollView) {
return RACTuplePack(scrollView, @(scrollView.contentOffset.x);
}] takeUntil:self.rac_willDeallocSignal]
setNameWithFormat:@"%@ -rac_didScrollHorizontally",
self.rac_description];
RACUseDelegateProxy(self);
return signal;
}
- (RACSignal *)rac_didScrollVertically {
RACSignal *signal = [[[[self.rac_delegateProxy signalForSelector:@selector(scrollViewDidScroll:)]
reduceEach:^RACTuple *(UIScrollView *scrollView) {
return RACTuplePack(scrollView, @(scrollView.contentOffset.y);
}] takeUntil:self.rac_willDeallocSignal]
setNameWithFormat:@"%@ -rac_didScrollVertically",
self.rac_description];
RACUseDelegateProxy(self);
return signal;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment