diff --git a/WKVerticalScrollBar.podspec b/WKVerticalScrollBar.podspec index 0989020..10a5604 100644 --- a/WKVerticalScrollBar.podspec +++ b/WKVerticalScrollBar.podspec @@ -12,4 +12,6 @@ Pod::Spec.new do |s| s.source_files = 'WKVerticalScrollBar/WKVerticalScrollBar.{h,m}' s.frameworks = 'QuartzCore' + + s.requires_arc = false end diff --git a/WKVerticalScrollBar/WKVerticalScrollBar.h b/WKVerticalScrollBar/WKVerticalScrollBar.h index 2b2133d..5ae8a1c 100644 --- a/WKVerticalScrollBar/WKVerticalScrollBar.h +++ b/WKVerticalScrollBar/WKVerticalScrollBar.h @@ -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; diff --git a/WKVerticalScrollBar/WKVerticalScrollBar.m b/WKVerticalScrollBar/WKVerticalScrollBar.m index f99a9e1..a876bdd 100644 --- a/WKVerticalScrollBar/WKVerticalScrollBar.m +++ b/WKVerticalScrollBar/WKVerticalScrollBar.m @@ -40,6 +40,8 @@ @implementation WKVerticalScrollBar @synthesize handleSelectedWidth = _handleSelectedWidth; @synthesize handleMinimumHeight = _handleMinimumHeight; +@synthesize useExplicitHandleHeight = _useExplicitHandleHeight; +@synthesize explicitHandleHeight = _explicitHandleHeight; - (id)initWithFrame:(CGRect)frame { @@ -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]; + } } } @@ -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; @@ -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, @@ -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; @@ -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))