Permalink
Browse files

Simulating the scroll view elasticity effect in the open state. refs 3

  • Loading branch information...
matej committed Apr 2, 2013
1 parent 3e9b893 commit d32b9460875b3f18216d41450e65bb23d782ac44
Showing with 19 additions and 3 deletions.
  1. +19 −3 MBPullDownController/MBPullDownController.m
@@ -23,7 +23,8 @@
@interface MBPullDownController ()
-@property MBPullDownControllerTapUpRecognizer *tapUpRecognizer;
+@property (nonatomic, strong) MBPullDownControllerTapUpRecognizer *tapUpRecognizer;
+@property (nonatomic, assign) BOOL adjustedScroll;
@end
@@ -308,7 +309,8 @@ - (void)tapUp:(MBPullDownControllerTapUpRecognizer *)recognizer {
#pragma mark - KVO
- (void)registerForScrollViewKVO:(UIScrollView *)scrollView {
- [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
+ self.adjustedScroll = NO;
+ [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)unregisterFromScrollViewKVO:(UIScrollView *)scrollView {
@@ -317,7 +319,21 @@ - (void)unregisterFromScrollViewKVO:(UIScrollView *)scrollView {
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {
- [self updateBackgroundViewForScrollOfset:[[change valueForKey:NSKeyValueChangeNewKey] CGPointValue]];
+ if (!self.adjustedScroll) {
+ CGPoint oldValue = [[change valueForKey:NSKeyValueChangeOldKey] CGPointValue];
+ CGPoint newValue = [[change valueForKey:NSKeyValueChangeNewKey] CGPointValue];
+ CGPoint adjusted = newValue;
+ // Simulate the scroll view elasticity effect while dragging in the open state
+ if (self.open && [self scrollView].dragging) {
+ CGFloat delta = roundf((oldValue.y - newValue.y) / 3);
+ adjusted = CGPointMake(newValue.x, oldValue.y - delta);
+ self.adjustedScroll = YES; // prevent infinite recursion
+ [self scrollView].contentOffset = adjusted;
+ }
+ [self updateBackgroundViewForScrollOfset:adjusted];
+ } else {
+ self.adjustedScroll = NO;
+ }
}
}

0 comments on commit d32b946

Please sign in to comment.