From 20676fb93739bfc7a29e708b490f0319b8071cf0 Mon Sep 17 00:00:00 2001 From: Tim Bodeit Date: Mon, 9 Nov 2015 21:39:52 +0100 Subject: [PATCH 1/4] Specify WKVerticalScrollBar as non-ARC project in podspec --- WKVerticalScrollBar.podspec | 2 ++ 1 file changed, 2 insertions(+) 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 From e5416f5a55de0f4618a5df83f0584671e2194f4c Mon Sep 17 00:00:00 2001 From: Tim Bodeit Date: Tue, 10 Nov 2015 00:25:23 +0100 Subject: [PATCH 2/4] Immediately update handle color when setting it --- WKVerticalScrollBar/WKVerticalScrollBar.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/WKVerticalScrollBar/WKVerticalScrollBar.m b/WKVerticalScrollBar/WKVerticalScrollBar.m index f99a9e1..b397429 100644 --- a/WKVerticalScrollBar/WKVerticalScrollBar.m +++ b/WKVerticalScrollBar/WKVerticalScrollBar.m @@ -139,10 +139,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]; + } } } From a9e0d8d5548318e4b80fec38dc75f6c44e77211b Mon Sep 17 00:00:00 2001 From: Tim Bodeit Date: Tue, 10 Nov 2015 00:25:57 +0100 Subject: [PATCH 3/4] Immediately update handle width when setting it --- WKVerticalScrollBar/WKVerticalScrollBar.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/WKVerticalScrollBar/WKVerticalScrollBar.m b/WKVerticalScrollBar/WKVerticalScrollBar.m index b397429..e3920ac 100644 --- a/WKVerticalScrollBar/WKVerticalScrollBar.m +++ b/WKVerticalScrollBar/WKVerticalScrollBar.m @@ -157,6 +157,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; From ce6fcf6b55996ea476050ff3710aac34f27c6ff1 Mon Sep 17 00:00:00 2001 From: Tim Bodeit Date: Tue, 10 Nov 2015 00:28:54 +0100 Subject: [PATCH 4/4] Allow setting an explicit handle height Example use case: Allows using a circle as a handle --- WKVerticalScrollBar/WKVerticalScrollBar.h | 12 ++++++++++ WKVerticalScrollBar/WKVerticalScrollBar.m | 27 ++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) 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 e3920ac..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 { @@ -215,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, @@ -241,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; @@ -315,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))