Skip to content

Instantly share code, notes, and snippets.

@fpillet
Created October 29, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpillet/04b46aa22edb516c7ee3 to your computer and use it in GitHub Desktop.
Save fpillet/04b46aa22edb516c7ee3 to your computer and use it in GitHub Desktop.
Example of encapsulating a gesture recognizer's behavior within a signal's callbacks
- (void)setupDraggableHeaderGestureRecognizer {
// setup a gesture recognizer so we can drag the "I need some daytime hours" header up and down
@weakify(self);
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] init];
_draggableHeaderView.userInteractionEnabled = YES;
[_draggableHeaderView addGestureRecognizer:recognizer];
RACDisposable *disposable = [[[recognizer
rac_gestureSignal]
scanWithStart:RACTuplePack(@(_mapViewHeightConstraint.constant), @0)
reduce:^(RACTuple *translations, UIPanGestureRecognizer *gestureRecognizer) {
@strongify(self);
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
return RACTuplePack(@(self.mapViewHeightConstraint.constant), @0);
case UIGestureRecognizerStateChanged: {
CGFloat verticalTranslation = floorf([gestureRecognizer translationInView:self.mapView.superview].y);
CGFloat initialConstant = ((NSNumber *) translations.first).floatValue;
CGFloat constant = initialConstant + verticalTranslation;
if (constant < [self minimumMapHeight])
constant = [self minimumMapHeight];
else if ((CGRectGetMinY(self.mapView.frame)+constant) > (CGRectGetMinY(self.searchButton.frame) - CGRectGetHeight(self.draggableHeaderView.frame))) {
constant = CGRectGetMinY(self.searchButton.frame) - CGRectGetHeight(self.draggableHeaderView.frame) - CGRectGetMinY(self.mapView.frame);
}
return RACTuplePack(translations.first, @(constant - initialConstant));
}
case UIGestureRecognizerStateCancelled:
return RACTuplePack(translations.first, translations.first);
default:
return translations;
}
}]
subscribeNext:^(RACTuple *tuple) {
@strongify(self);
self.mapViewHeightConstraint.constant = ((NSNumber *) tuple.first).floatValue + ((NSNumber *) tuple.second).floatValue;
}];
_draggableHeaderDisposable = [RACDisposable disposableWithBlock:^{
[_draggableHeaderView removeGestureRecognizer:recognizer];
[disposable dispose];
}].asScopedDisposable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment