Skip to content

Instantly share code, notes, and snippets.

@toulouse
Created May 7, 2013 07:07
Show Gist options
  • Save toulouse/5530755 to your computer and use it in GitHub Desktop.
Save toulouse/5530755 to your computer and use it in GitHub Desktop.
Augments a UITableViewDelegate to let you detect which direction a UITableView has been scrolled.
// Created by Andrew Toulouse on 5/6/13.
// Copyright (c) 2013 Andrew Toulouse. All rights reserved.
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, ATScrollDirection) {
ATScrollDirectionUp,
ATScrollDirectionDown
};
@interface ATScrollDirectionProxy : NSProxy <UITableViewDelegate>
@property (nonatomic, assign) ATScrollDirection direction;
- (id)initWithTableViewDelegate:(id<UITableViewDelegate>)tableViewDelegate;
@end
// Created by Andrew Toulouse on 5/6/13.
// Copyright (c) 2013 Andrew Toulouse. All rights reserved.
#import "ATScrollDirectionProxy.h"
@interface ATScrollDirectionProxy ()
@property (nonatomic, strong) id<NSObject, UITableViewDelegate> tableViewDelegate;
@property (nonatomic, assign) BOOL trackOffset;
@property (nonatomic, assign) CGFloat lastScrollOffset;
@property (nonatomic, assign) CGFloat velocity;
@end
@implementation ATScrollDirectionProxy
- (id)initWithTableViewDelegate:(id<NSObject, UITableViewDelegate>)tableViewDelegate {
if (self) {
_tableViewDelegate = tableViewDelegate;
}
return self;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:_tableViewDelegate];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [[_tableViewDelegate class] instanceMethodSignatureForSelector:sel];
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol {
return [_tableViewDelegate conformsToProtocol:aProtocol];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
if (![[self class] instancesRespondToSelector:aSelector]) {
return [[_tableViewDelegate class] instancesRespondToSelector:aSelector];
}
return YES;
}
#pragma mark - UIScrollViewDelegate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.lastScrollOffset = scrollView.contentOffset.y;
self.trackOffset = YES;
if ([self.tableViewDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[self.tableViewDelegate scrollViewWillBeginDragging:scrollView];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
self.velocity = velocity.y;
self.trackOffset = NO;
if ([self.tableViewDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {
[self.tableViewDelegate scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.trackOffset) {
self.velocity = scrollView.contentOffset.y - self.lastScrollOffset;
self.lastScrollOffset = scrollView.contentOffset.y;
}
if (self.velocity < 0) {
self.direction = ATScrollDirectionUp;
} else {
self.direction = ATScrollDirectionDown;
}
if ([self.tableViewDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[self.tableViewDelegate scrollViewDidScroll:scrollView];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment