Skip to content
Open
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
69 changes: 43 additions & 26 deletions RLPageControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
@interface RLPageControl ()
{
@private

NSUInteger _currentPage;
NSUInteger _numberOfPages;

BOOL _hidesForSinglePage;

RLDrawIndicatorBlock _indicatorDrawBlock;
}

Expand All @@ -39,7 +39,7 @@ - (void)_sharedInit
self.numberOfPages = 0;
self.hidesForSinglePage = NO;
self.opaque = NO;

[self setIndicatorDrawBlock:^(CGContextRef context, NSUInteger indicatorIndex, BOOL isHighlighted){
if (isHighlighted) {
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 8, 8)];
Expand All @@ -51,15 +51,15 @@ - (void)_sharedInit
[ovalPath fill];
}
}];


UITapGestureRecognizer *tapGuesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_userDidTap:)];
UISwipeGestureRecognizer *leftSwipeGuesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(_userDidSwipeLeft:)];
UISwipeGestureRecognizer *rightSwipeGuesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(_userDidSwipeRight:)];

[leftSwipeGuesture setDirection:UISwipeGestureRecognizerDirectionLeft];
[rightSwipeGuesture setDirection:UISwipeGestureRecognizerDirectionRight];

[self addGestureRecognizer:tapGuesture];
[self addGestureRecognizer:leftSwipeGuesture];
[self addGestureRecognizer:rightSwipeGuesture];
Expand All @@ -68,20 +68,20 @@ - (void)_sharedInit
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

if (self)
[self _sharedInit];

return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

if (self)
[self _sharedInit];

return self;
}

Expand All @@ -91,18 +91,35 @@ - (id)initWithCoder:(NSCoder *)aDecoder

- (void)setCurrentPage:(NSUInteger)currentPage
{
currentPage = [self _limitPageNumber:currentPage];

if (currentPage == _currentPage)
return;

_currentPage = currentPage;
[self setNeedsDisplay];
}

- (NSUInteger)_limitPageNumber:(NSUInteger)page {

if (self.numberOfPages == 0)
return 0;

NSInteger currentPageSigned = (NSInteger) page; // Assumes numberOfPages does not exceed NSIntegerMax
if (currentPageSigned < 0)
return 0;

if (page >= self.numberOfPages)
return self.numberOfPages - 1;

return page;
}

- (void)setNumberOfPages:(NSUInteger)numberOfPages
{
if (numberOfPages == _numberOfPages)
return;

_numberOfPages = numberOfPages;
[self setNeedsDisplay];
}
Expand All @@ -111,7 +128,7 @@ - (void)setHidesForSinglePage:(BOOL)hidesForSinglePage
{
if (hidesForSinglePage == _hidesForSinglePage)
return;

_hidesForSinglePage = hidesForSinglePage;
[self setNeedsDisplay];
}
Expand All @@ -132,44 +149,44 @@ - (void)_userDidTap:(id)sender
{
CGFloat midX = CGRectGetMidX(self.bounds);
CGPoint touchLocation = [sender locationInView:self];

if (touchLocation.x < midX)
[self setCurrentPage:MAX(self.currentPage - 1, 0)];
self.currentPage--;
else
[self setCurrentPage:MIN(self.currentPage + 1, self.numberOfPages - 1)];

self.currentPage++;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)_userDidSwipeLeft:(id)sender
{
[self setCurrentPage:MAX(self.currentPage - 1, 0)];
self.currentPage--;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)_userDidSwipeRight:(id)sender
{
[self setCurrentPage:MIN(self.currentPage + 1, self.numberOfPages - 1)];
self.currentPage++;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)drawRect:(CGRect)rect
{
if (self.hidesForSinglePage && self.numberOfPages == 1)
return;

CGContextRef context = UIGraphicsGetCurrentContext();

CGSize size = [self sizeForNumberOfPages:self.numberOfPages];
CGFloat startDrawX = CGRectGetMidX(self.bounds) - size.width / 2;

CGContextTranslateCTM(context, startDrawX, 0);

for (int i = 0; i < self.numberOfPages; i++)
{
if (i > 0)
CGContextTranslateCTM(context, SIZE_OF_INDICATOR, 0);

CGContextSaveGState(context);
_indicatorDrawBlock(context, i, self.currentPage == i);
CGContextRestoreGState(context);
Expand Down