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

Indicator stroke color and width options #27

Open
wants to merge 3 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
27 changes: 16 additions & 11 deletions SMPageControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ typedef NS_ENUM(NSUInteger, SMPageControlTapBehavior) {

@property (nonatomic) NSInteger numberOfPages;
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) CGFloat indicatorMargin UI_APPEARANCE_SELECTOR; // deafult is 10
@property (nonatomic) CGFloat indicatorDiameter UI_APPEARANCE_SELECTOR; // deafult is 6
@property (nonatomic) CGFloat minHeight UI_APPEARANCE_SELECTOR; // default is 36, cannot be less than indicatorDiameter
@property (nonatomic) SMPageControlAlignment alignment UI_APPEARANCE_SELECTOR; // deafult is Center
@property (nonatomic) SMPageControlVerticalAlignment verticalAlignment UI_APPEARANCE_SELECTOR; // deafult is Middle

@property (nonatomic, strong) UIImage *pageIndicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIImage *pageIndicatorMaskImage UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set
@property (nonatomic, strong) UIColor *pageIndicatorTintColor UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set
@property (nonatomic, strong) UIImage *currentPageIndicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *currentPageIndicatorTintColor UI_APPEARANCE_SELECTOR; // ignored if currentPageIndicatorImage is set
@property (nonatomic) CGFloat indicatorMargin UI_APPEARANCE_SELECTOR; // deafult is 10
@property (nonatomic) CGFloat indicatorDiameter UI_APPEARANCE_SELECTOR; // deafult is 6
@property (nonatomic) CGFloat minHeight UI_APPEARANCE_SELECTOR; // default is 36, cannot be less than indicatorDiameter
@property (nonatomic) SMPageControlAlignment alignment UI_APPEARANCE_SELECTOR; // deafult is Center
@property (nonatomic) SMPageControlVerticalAlignment verticalAlignment UI_APPEARANCE_SELECTOR; // deafult is Middle

@property (nonatomic, strong) UIImage *pageIndicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIImage *pageIndicatorMaskImage UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set
@property (nonatomic, strong) UIColor *pageIndicatorTintColor UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set
@property (nonatomic, strong) UIColor *pageIndicatorTintStrokeColor UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set
@property (nonatomic, assign) CGFloat pageIndicatorTintStrokeWidth UI_APPEARANCE_SELECTOR; // ignored if pageIndicatorImage is set

@property (nonatomic, strong) UIImage *currentPageIndicatorImage UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *currentPageIndicatorTintColor UI_APPEARANCE_SELECTOR; // ignored if currentPageIndicatorImage is set
@property (nonatomic, strong) UIColor *currentPageIndicatorTintStrokeColor UI_APPEARANCE_SELECTOR; // ignored if currentPageIndicatorImage is set
@property (nonatomic, assign) CGFloat currentPageIndicatorTintStrokeWidth UI_APPEARANCE_SELECTOR; // ignored if currentPageIndicatorImage is set

@property (nonatomic) BOOL hidesForSinglePage; // hide the the indicator if there is only one page. default is NO
@property (nonatomic) BOOL defersCurrentPageDisplay; // if set, clicking to a new page won't update the currently displayed page until -updateCurrentPageDisplay is called. default is NO
Expand Down
18 changes: 17 additions & 1 deletion SMPageControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ - (void)_initialize
{
_numberOfPages = 0;
_tapBehavior = SMPageControlTapBehaviorStep;

_currentPageIndicatorTintStrokeWidth = 1.0f;
_pageIndicatorTintStrokeWidth = 1.0f;

self.backgroundColor = [UIColor clearColor];

Expand Down Expand Up @@ -146,6 +149,8 @@ - (void)_renderPages:(CGContextRef)context rect:(CGRect)rect
CGFloat xOffset = left;
CGFloat yOffset = 0.0f;
UIColor *fillColor = nil;
UIColor *strokeColor = nil;
CGFloat strokeWidth = 1.0f;
UIImage *image = nil;
CGImageRef maskingImage = nil;
CGSize maskSize = CGSizeZero;
Expand All @@ -155,12 +160,16 @@ - (void)_renderPages:(CGContextRef)context rect:(CGRect)rect

if (i == _displayedPage) {
fillColor = _currentPageIndicatorTintColor ? _currentPageIndicatorTintColor : [UIColor whiteColor];
strokeColor = _currentPageIndicatorTintStrokeColor;
strokeWidth = _currentPageIndicatorTintStrokeWidth;
image = _currentPageImages[indexNumber];
if (nil == image) {
image = _currentPageIndicatorImage;
}
} else {
fillColor = _pageIndicatorTintColor ? _pageIndicatorTintColor : [[UIColor whiteColor] colorWithAlphaComponent:0.3f];
strokeColor = _pageIndicatorTintStrokeColor;
strokeWidth = _pageIndicatorTintStrokeWidth;
image = _pageImages[indexNumber];
if (nil == image) {
image = _pageIndicatorImage;
Expand All @@ -180,7 +189,8 @@ - (void)_renderPages:(CGContextRef)context rect:(CGRect)rect
}
}

[fillColor set];
[fillColor setFill];
if (strokeColor) [strokeColor setStroke];
CGRect indicatorRect;
if (image) {
yOffset = [self _topOffsetForHeight:image.size.height rect:rect];
Expand All @@ -197,6 +207,10 @@ - (void)_renderPages:(CGContextRef)context rect:(CGRect)rect
CGFloat centeredXOffset = xOffset + floorf((_measuredIndicatorWidth - _indicatorDiameter) / 2.0f);
indicatorRect = CGRectMake(centeredXOffset, yOffset, _indicatorDiameter, _indicatorDiameter);
CGContextFillEllipseInRect(context, indicatorRect);
if (strokeColor) {
CGContextSetLineWidth(context, strokeWidth);
CGContextStrokeEllipseInRect(context, CGRectInset(indicatorRect, strokeWidth * 0.5f, strokeWidth * 0.5f));
}
}

[pageRects addObject:[NSValue valueWithCGRect:indicatorRect]];
Expand Down Expand Up @@ -404,13 +418,15 @@ - (void)setStyleWithDefaults:(SMPageControlStyleDefaults)defaultStyle
self.indicatorDiameter = DEFAULT_INDICATOR_WIDTH_LARGE;
self.indicatorMargin = DEFAULT_INDICATOR_MARGIN_LARGE;
self.pageIndicatorTintColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2f];
self.pageIndicatorTintStrokeColor = nil;
self.minHeight = DEFAULT_MIN_HEIGHT_LARGE;
break;
case SMPageControlDefaultStyleClassic:
default:
self.indicatorDiameter = DEFAULT_INDICATOR_WIDTH;
self.indicatorMargin = DEFAULT_INDICATOR_MARGIN;
self.pageIndicatorTintColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3f];
self.pageIndicatorTintStrokeColor = nil;
self.minHeight = DEFAULT_MIN_HEIGHT;
break;
}
Expand Down