0

这个可以吗?

- (RACSignal *)deferRefreshSignalUntilScrollViewIsNotBusy:(RACSignal *)infiniteSignal
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        __block BOOL disposed = NO;
        RACDisposable *disposable = [infiniteSignal subscribeNext:^(id originalValue) {
            dispatch_repeated(0.0, 0.5, dispatch_get_main_queue(), ^(BOOL *stop) {

                BOOL scrollViewIsBusy = self.scrollView.isTracking
                   || self.scrollView.isDecelerating
                   || self.scrollView.isDragging
                   || _flags.animatingArticlePageScroller;

                *stop = disposed;

                if(!scrollViewIsBusy)
                {
                    [subscriber sendNext:originalValue];
                    *stop = YES;
                }
            });
        }];
        return [RACDisposable disposableWithBlock:^{
            [disposable dispose];
            disposed = YES;
        }];
    }];
}

static void dispatch_repeated_internal(dispatch_time_t firstPopTime, double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop))
{    
    __block BOOL shouldStop = NO;
    dispatch_time_t nextPopTime = dispatch_time(firstPopTime, (int64_t)intervalInSeconds * NSEC_PER_SEC);
    dispatch_after(nextPopTime, queue, ^{
        work(&shouldStop);
        if(!shouldStop)
        {
            dispatch_repeated_internal(nextPopTime, intervalInSeconds, queue, work);
        }
    });
}

void dispatch_repeated(double delay, double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop))
{
    dispatch_time_t firstPopTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)delay * NSEC_PER_SEC);
    dispatch_repeated_internal(firstPopTime, intervalInSeconds, queue, work);
}

重要的是不要一直轮询滚动视图值。仅在您现在有新值时进行轮询。

4

2 回答 2

3
  1. 将滚动视图的委托连接到self(假设self是 的子类UIViewController

  2. 将此代码添加到self... 该信号@YES在滚动视图滚动时返回。@NO停止时它将返回。

    - (RACSignal *)makeScrollViewSignal
    {
        RACSignal *didScroll = [self rac_signalForSelector:@selector(scrollViewDidScroll:) fromProtocol:@protocol(UIScrollViewDelegate)];
        RACSignal *willDrag = [self rac_signalForSelector:@selector(scrollViewWillBeginDragging:) fromProtocol:@protocol(UIScrollViewDelegate)];
        RACSignal *didDrag = [[self rac_signalForSelector:@selector(scrollViewDidEndDragging:willDecelerate:) fromProtocol:@protocol(UIScrollViewDelegate)] filter:^BOOL(RACTuple *tuple) {
            return [tuple.second isEqualToNumber:@NO];
        }];
        RACSignal *didDecelerate = [self rac_signalForSelector:@selector(scrollViewDidEndDecelerating:) fromProtocol:@protocol(UIScrollViewDelegate)];
    
        return [RACSignal merge:@[[didScroll mapReplace:@YES],
                                  [willDrag mapReplace:@YES],
                                  [didDrag mapReplace:@NO],
                                  [didDecelerate mapReplace:@NO],
                                  ]];
    }
    

3A。如果您想丢弃next除最新的以外的所有内容,...

感谢Justin Spahr-Summers提供解决方案!

__block BOOL isScrolling = NO;

[self.scrollingSignal subscribeNext:^(NSNumber *isScrollingNumber) {
    isScrolling = [isScrollingNumber boolValue];
}];

// your signal here
[[self.timerSignal throttle:INFINITY valuesPassingTest:^BOOL(id next) {
    return isScrolling;
}] subscribeNext:^(NSNumber *timeInterval) {
    // your work here
    [self log:[NSString stringWithFormat:@"Timer: %ld", [timeInterval integerValue]]];
}];

3B。如果你想收到所有next,...

解决方案 3B-1

子类RACQueueScheduler

@interface DelayedScheduler : RACQueueScheduler
@property (nonatomic, strong) dispatch_queue_t delayQueue;
@property (nonatomic, strong) RACDisposable *delayDisposable;
@end


@implementation DelayedScheduler

+ (instancetype)delayedSchedulerWithSignal:(RACSignal *)signal
{
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
    DelayedScheduler *obj = [[self alloc] initWithName:@"com.ReactiveCocoa.RACScheduler.delayedMainThreadScheduler" queue:queue];

    obj.delayQueue = queue;
    obj.delayDisposable = [signal subscribeNext:^(NSNumber *delayEnabled) {
        if ([delayEnabled boolValue]) {
            dispatch_suspend(obj.queue);
        } else {
            dispatch_resume(obj.queue);
        }
    } error:^(NSError *error) {
        dispatch_resume(obj.queue);
        obj.delayDisposable = nil;
    } completed:^{
        dispatch_resume(obj.queue);
        obj.delayDisposable = nil;
    }];

    return obj;
}

- (void)dealloc
{
    [self.delayDisposable dispose];
}

@end

然后更改您的信号以在新的调度程序上交付

DelayedScheduler *delayed = [DelayedScheduler delayedSchedulerWithSignal:self.scrollingSignal];
[[self.timerSignal deliverOn:delayed] subscribeNext:^(NSNumber *timeInterval) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self log:[NSString stringWithFormat:@"%@", timeInterval]];
    });
}];

解决方案 3B-2

将此添加到viewDidLoad...

[[self makeScrollViewSignal] subscribeNext:^(NSNumber *isScrolling) {
    if ([isScrolling boolValue]) {
        dispatch_suspend(self.queue);
    } else {
        dispatch_resume(self.queue);
    }
}];

// Your signal here... change the `self.timerSignal` to anything else
[self.timerSignal subscribeNext:^(NSNumber *timeInterval) {
    dispatch_async(self.queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            // TODO: add your code here...
            [self log:[NSString stringWithFormat:@"%@", timeInterval]];
        });
    });
}];
于 2014-02-25T06:42:06.303 回答
2

出于好奇,为什么不将自己(或其他合适的对象)作为 UIScrollViewDelegate 提供给滚动视图,并使用

[self rac_signalForSelector:@selector(scrollViewDidEndScrollingAnimation:)
               fromProtocol:@protocol(UIScrollViewDelegate)]

在滚动视图完成滚动时获得通知您的信号?

于 2014-02-25T04:41:18.610 回答