如何将ReactiveCocoa与手势识别器一起使用

时间:2013-05-01 14:50:20

标签: ios objective-c reactive-programming reactive-cocoa

我正在使用ReactiveCocoa构建应用程序。顶视图是一个菜单,可以向下拉,然后向上推。我必须使用两种不同的手势识别器 - 一种用于拉下,另一种用于推回。一次只能启用一个 - 这就是我的问题。州。

我正在使用BlocksKit扩展来设置手势识别器。

self.panHeaderDownGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
    UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)sender;

    CGPoint translation = [recognizer translationInView:self.view];

    if (state == UIGestureRecognizerStateChanged)
    {
        [self.downwardHeaderPanSubject sendNext:@(translation.y)];
    }
    else if (state == UIGestureRecognizerStateEnded)
    {
        // Determine the direction the finger is moving and ensure if it was moving down, that it exceeds the minimum threshold for opening the menu.
        BOOL movingDown = ([recognizer velocityInView:self.view].y > 0 && translation.y > kMoveDownThreshold);

        // Animate the change
        [UIView animateWithDuration:0.25f animations:^{
            if (movingDown)
            {
                [self.downwardHeaderPanSubject sendNext:@(kMaximumHeaderTranslationThreshold)];
            }
            else
            {
                [self.downwardHeaderPanSubject sendNext:@(0)];
            }
        } completion:^(BOOL finished) {
            [self.menuFinishedTransitionSubject sendNext:@(movingDown)];
        }];
    }
}];

在我的initWithNibName:bundle:方法中,我正在设置以下RACSubject

self.headerMovementSubject = [RACSubject subject];
[self.headerMovementSubject subscribeNext:^(id x) {
    @strongify(self);

    // This is the ratio of the movement. 0 is closed and 1 is open.
    // Values less than zero are treated as zero.
    // Values greater than one are valid and will be extrapolated beyond the fully open menu.
    CGFloat ratio = [x floatValue];

    CGRect headerFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), kHeaderHeight + ratio * kMaximumHeaderTranslationThreshold);

    if (ratio < 0)
    {            
        headerFrame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), kHeaderHeight);
    }

    self.headerViewController.view.frame = headerFrame;
}];

// This subject is responsible for receiving translations from a gesture recognizers and turning
// thos values into ratios. These ratios are fead into other signals.
self.downwardHeaderPanSubject = [RACSubject subject];
[self.downwardHeaderPanSubject subscribeNext:^(NSNumber *translation) {
    @strongify(self);
    CGFloat verticalTranslation = [translation floatValue];

    CGFloat effectiveRatio = 0.0f;

    if (verticalTranslation <= 0)
    {
        effectiveRatio = 0.0f;
    }
    else if (verticalTranslation <= kMaximumHeaderTranslationThreshold)
    {
        effectiveRatio = fabsf(verticalTranslation / kMaximumHeaderTranslationThreshold);
    }
    else
    {
        CGFloat overshoot = verticalTranslation - kMaximumHeaderTranslationThreshold;
        CGFloat y = 2 * sqrtf(overshoot + 1) - 2;
        effectiveRatio = 1.0f + (y / kMaximumHeaderTranslationThreshold);
    }

    [self.headerMovementSubject sendNext:@(effectiveRatio)];
}];

// This subject is responsible for mapping this value to other signals and state (ugh). 
self.menuFinishedTransitionSubject = [RACReplaySubject subject];
[self.menuFinishedTransitionSubject subscribeNext:^(NSNumber *menuIsOpenNumber) {
    @strongify(self);

    BOOL menuIsOpen = menuIsOpenNumber.boolValue;

    self.panHeaderDownGestureRecognizer.enabled = !menuIsOpen;
    self.panHeaderUpGestureRecognizer.enabled = menuIsOpen;
    self.otherViewController.view.userInteractionEnabled = !menuIsOpen;

    if (menuIsOpen)
    {
        [self.headerViewController flashScrollBars];
    }
}];

这里有很多事情要发生。我已经将这里列出的主题数量增加了近一倍(用于泛起手势识别器的主题), plus 另一组识别器,这个问题更加严重。与页脚的类似交互。这是很多科目。

我的问题分为两部分:

  1. 有没有更好的方法来设置我想要的链接类型?我在我的俯卧姿势中重新使用了一些主题,看起来非常相似。我有很多RACSubjects,看起来很笨拙。
  2. menuFinishedTransitionSubject主要用于管理手势识别器的状态。我试图绑定他们的enabled财产而没有任何运气。有什么建议吗?

1 个答案:

答案 0 :(得分:5)

让我们专注于显式订阅,因为这些通常是重写命令式代码的最佳结果。

首先,根据显示的代码,看起来headerMovementSubject仅从downwardHeaderPanSubject(以及其他地方)输入值。这是一个轻松的写作转换的候选人:

RACSignal *headerFrameSignal = [[self.downwardHeaderPanSubject
    map:^(NSNumber *translation) {
        CGFloat verticalTranslation = [translation floatValue];
        CGFloat effectiveRatio = 0.0f;

        // Calculate effectiveRatio.

        return @(effectiveRatio);
    }]
    map:^(NSNumber *effectiveRatio) {
        // Calculate headerFrame.

        return @(headerFrame);
    }];

然后,我们可以使用绑定来代替操纵self.headerViewController.view.frame作为副作用:

RAC(self.headerViewController.view.frame) = headerFrameSignal;

我们可以在menuFinishedTransitionSubject中使用布尔值做类似的事情:

RAC(self.panHeaderDownGestureRecognizer.enabled) = [self.menuFinishedTransitionSubject not];
RAC(self.panHeaderUpGestureRecognizer.enabled) = self.menuFinishedTransitionSubject;
RAC(self.otherViewController.view.userInteractionEnabled) = [self.menuFinishedTransitionSubject not];

不幸的是,-flashScrollBars仍然需要作为副作用调用,但我们至少可以将过滤从块中取出:

[[self.menuFinishedTransitionSubject
    filter:^(NSNumber *menuIsOpen) {
        return menuIsOpen.boolValue;
    }]
    subscribeNext:^(id _) {
        @strongify(self);

        [self.headerViewController flashScrollBars];
    }];

如果你想变得非常花哨,很多手势识别器逻辑可以用流变换来表示,动画可以用ReactiveCocoaLayout来实现,但这是对它自己的重写。