Created
December 13, 2011 07:11
-
-
Save drance/1471010 to your computer and use it in GitHub Desktop.
PoC for an accessory view that tracks scrolling a la Path
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
// ScrollieScrollView | |
// PoC for an accessory view that tracks scrolling | |
// Created by Matt Drance (@drance) on 12/12/11. | |
@interface ScrollieScrollView : UIScrollView () | |
@property (strong, nonatomic) UIView *thumb; | |
@end | |
@implementation ScrollieScrollView | |
@synthesize thumb = _thumb; | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
if (self.thumb == nil) { | |
self.thumb = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80.0, 20.0)]; | |
self.thumb.backgroundColor = [UIColor greenColor]; | |
[self addSubview:self.thumb]; | |
} | |
CGRect thumbFrame = self.thumb.frame; | |
CGFloat scrollDistance = self.contentSize.height - self.frame.size.height; | |
CGFloat percentScrolled = self.contentOffset.y / scrollDistance; | |
CGFloat projectedThumbY = floor(self.contentOffset.y + (self.frame.size.height * percentScrolled)); | |
CGFloat maxThumbY = (self.contentOffset.y + self.frame.size.height) - thumbFrame.size.height; | |
CGFloat minThumbY = 0; | |
CGFloat thumbY = MAX(projectedThumbY, minThumbY); | |
thumbY = MIN(thumbY, maxThumbY); | |
if (self.contentOffset.y < 0) { | |
thumbY = self.contentOffset.y; | |
} else { | |
CGFloat overshoot = self.contentOffset.y - (self.contentSize.height - self.frame.size.height); | |
if (overshoot > 0) { | |
thumbY = (self.contentSize.height - thumbFrame.size.height) + overshoot; | |
} | |
} | |
thumbFrame.origin.y = thumbY; | |
self.thumb.frame = thumbFrame; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment