Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting an explicit handle height #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WKVerticalScrollBar.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ Pod::Spec.new do |s|
s.source_files = 'WKVerticalScrollBar/WKVerticalScrollBar.{h,m}'

s.frameworks = 'QuartzCore'

s.requires_arc = false
end
12 changes: 12 additions & 0 deletions WKVerticalScrollBar/WKVerticalScrollBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@

@property (nonatomic, readwrite) CGFloat handleMinimumHeight;

/**
* Whether to use a custom height for the handle.
*
* @param false Handle height proportional to the visible portion of the scrollview content
* @param true Use custom handle height
*/
@property (nonatomic, readwrite) BOOL useExplicitHandleHeight;
/**
* Custom height of the handle, if useExplicitHandleHeight is enabled.
*/
@property (nonatomic, readwrite) CGFloat explicitHandleHeight;

@property (nonatomic, readwrite, retain) UIScrollView *scrollView;
@property (nonatomic, readwrite, retain) UIView *handleAccessoryView;

Expand Down
42 changes: 36 additions & 6 deletions WKVerticalScrollBar/WKVerticalScrollBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ @implementation WKVerticalScrollBar
@synthesize handleSelectedWidth = _handleSelectedWidth;

@synthesize handleMinimumHeight = _handleMinimumHeight;
@synthesize useExplicitHandleHeight = _useExplicitHandleHeight;
@synthesize explicitHandleHeight = _explicitHandleHeight;

- (id)initWithFrame:(CGRect)frame
{
Expand Down Expand Up @@ -139,10 +141,16 @@ - (void)setHandleColor:(UIColor *)color forState:(UIControlState)state
[color retain];
[normalColor release];
normalColor = color;
if (!handleDragged) {
[handle setBackgroundColor: color.CGColor];
}
} else if (state == UIControlStateSelected) {
[color retain];
[selectedColor release];
selectedColor = color;
if (handleDragged) {
[handle setBackgroundColor: color.CGColor];
}
}
}

Expand All @@ -151,6 +159,15 @@ - (CGFloat)handleCornerRadius
return _handleCornerRadius;
}

- (void)setHandleWidth:(CGFloat)handleWidth
{
_handleWidth = handleWidth;

CGRect handleFrame = handle.frame;
handleFrame.size.width = handleWidth;
handle.frame = handleFrame;
}

- (void)setHandleCornerRadius:(CGFloat)handleCornerRadius
{
_handleCornerRadius = handleCornerRadius;
Expand Down Expand Up @@ -200,10 +217,7 @@ - (void)layoutSubviews
CGFloat scrollValue = (contentHeight - frameHeight == 0) ? 0
: [_scrollView contentOffset].y / (contentHeight - frameHeight);

// Set handleHeight proportionally to how much content is being currently viewed
CGFloat handleHeight = CLAMP((frameHeight / contentHeight) * bounds.size.height,
_handleMinimumHeight, bounds.size.height);

CGFloat handleHeight = [self handleHeight];
[handle setOpacity:(handleHeight == bounds.size.height) ? 0.0f : 1.0f];

// Not only move the handle, but also shift where the position maps on to the handle,
Expand All @@ -226,6 +240,22 @@ - (void)layoutSubviews
[CATransaction commit];
}

- (CGFloat)handleHeight
{
if (_useExplicitHandleHeight) {
return _explicitHandleHeight;
} else {
CGFloat contentHeight = [_scrollView contentSize].height;
CGFloat frameHeight = [_scrollView frame].size.height;
CGRect bounds = [self bounds];

// Set handleHeight proportionally to how much content is being currently viewed
return CLAMP((frameHeight/ contentHeight) * bounds.size.height,
_handleMinimumHeight,
bounds.size.height);
}
}

- (BOOL)handleVisible
{
return [handle opacity] == 1.0f;
Expand Down Expand Up @@ -300,8 +330,8 @@ - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
CGSize contentSize = [_scrollView contentSize];
CGPoint contentOffset = [_scrollView contentOffset];
CGFloat frameHeight = [_scrollView frame].size.height;
CGFloat deltaY = ((point.y - lastTouchPoint.y) / [self bounds].size.height)
* [_scrollView contentSize].height;
CGFloat deltaY = (point.y - lastTouchPoint.y) / (self.bounds.size.height-[self handleHeight])
* (contentSize.height-frameHeight);

[_scrollView setContentOffset:CGPointMake(contentOffset.x, CLAMP(contentOffset.y + deltaY,
0, contentSize.height - frameHeight))
Expand Down