Last active
December 28, 2015 11:49
-
-
Save hyperspacemark/7495991 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface ATParallaxTableHeaderView : UIView | |
| @property (weak, nonatomic, readonly) UIView *contentView; | |
| - (id)initWithContentView:(UIView *)contentView; | |
| - (void)attachToTableView:(UITableView *)tableView; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ATParallaxTableHeaderView.h" | |
| static void *ATParallaxTableHeaderViewObserverContext = &ATParallaxTableHeaderViewObserverContext; | |
| @interface ATParallaxTableHeaderView () | |
| @property (weak, nonatomic, readwrite) UIView *contentView; | |
| @property (weak, nonatomic) UITableView *tableView; | |
| @end | |
| @implementation ATParallaxTableHeaderView | |
| #pragma mark - Lifecycle | |
| - (id)initWithContentView:(UIView *)contentView | |
| { | |
| NSParameterAssert(contentView); | |
| if (!(self = [super initWithFrame:contentView.bounds])) { | |
| return nil; | |
| } | |
| [self addSubview:contentView]; | |
| _contentView = contentView; | |
| return self; | |
| } | |
| - (void)dealloc | |
| { | |
| [self removeObserver:self forKeyPath:self.observerKeyPath context:ATParallaxTableHeaderViewObserverContext]; | |
| } | |
| #pragma mark - API | |
| #pragma mark Hooking Onto the Table View | |
| - (void)attachToTableView:(UITableView *)tableView | |
| { | |
| NSParameterAssert(tableView); | |
| self.tableView = tableView; | |
| [self configureTableView]; | |
| [self observeTableViewContentOffsetChanges]; | |
| } | |
| - (void)configureTableView | |
| { | |
| self.tableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.bounds), 0, 0, 0); | |
| [self.tableView addSubview:self]; | |
| } | |
| #pragma mark Responding to Table View Content Offset Changes | |
| - (void)observeTableViewContentOffsetChanges | |
| { | |
| [self.tableView addObserver:self forKeyPath:self.observerKeyPath options:NSKeyValueObservingOptionNew context:ATParallaxTableHeaderViewObserverContext]; | |
| } | |
| #pragma mark KVO | |
| - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context | |
| { | |
| if (context != ATParallaxTableHeaderViewObserverContext) { | |
| return; | |
| } | |
| CGPoint contentOffset = [change[NSKeyValueChangeNewKey] CGPointValue]; | |
| if (contentOffset.y < -CGRectGetHeight(self.bounds)) { | |
| CGRect frame = self.contentView.frame; | |
| frame.origin.y = contentOffset.y; | |
| frame.size.height = -contentOffset.y; | |
| self.contentView.frame = frame; | |
| } | |
| } | |
| - (NSString *)observerKeyPath | |
| { | |
| static NSString *observerKeyPath; | |
| if (!observerKeyPath) { | |
| observerKeyPath = NSStringFromSelector(@selector(contentOffset)); | |
| } | |
| return observerKeyPath; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment