Ash*_*row 5 objective-c reactive-programming ios 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)];
}];
}
}];
Run Code Online (Sandbox Code Playgroud)
在我的initWithNibName:bundle:方法中,我正在设置以下RACSubjects.
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];
}
}];
Run Code Online (Sandbox Code Playgroud)
这里有很多事情要做.这个问题因为我已经在这里列出的主题数量几乎翻了一倍(用于泛起手势识别器的主题),再加上另一组用于与页脚进行类似交互的识别器而加剧了这个问题.这是很多科目.
我的问题分为两部分:
RACSubjects看起来很笨拙.menuFinishedTransitionSubject基本上用于管理手势识别器的状态.我试图绑定他们的enabled财产没有任何运气.有什么建议吗?让我们关注显式订阅,因为这些通常是重写命令式代码的最佳结果.
首先,根据显示的代码,它看起来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);
}];
Run Code Online (Sandbox Code Playgroud)
然后,self.headerViewController.view.frame我们可以使用绑定而不是作为副作用进行操作:
RAC(self.headerViewController.view.frame) = headerFrameSignal;
Run Code Online (Sandbox Code Playgroud)
我们可以在布尔代表中做类似的事情menuFinishedTransitionSubject:
RAC(self.panHeaderDownGestureRecognizer.enabled) = [self.menuFinishedTransitionSubject not];
RAC(self.panHeaderUpGestureRecognizer.enabled) = self.menuFinishedTransitionSubject;
RAC(self.otherViewController.view.userInteractionEnabled) = [self.menuFinishedTransitionSubject not];
Run Code Online (Sandbox Code Playgroud)
不幸的是,-flashScrollBars仍然需要调用副作用,但我们至少可以解除块的过滤:
[[self.menuFinishedTransitionSubject
filter:^(NSNumber *menuIsOpen) {
return menuIsOpen.boolValue;
}]
subscribeNext:^(id _) {
@strongify(self);
[self.headerViewController flashScrollBars];
}];
Run Code Online (Sandbox Code Playgroud)
如果你想变得非常花哨,很多手势识别器逻辑可以用流转换来表示,动画可以用ReactiveCocoaLayout实现,但这是自己的重写.
| 归档时间: |
|
| 查看次数: |
3444 次 |
| 最近记录: |