Skip to content
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
40 changes: 40 additions & 0 deletions iCarousel/PageControl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// PageControl.h
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//

#import <UIKit/UIKit.h>

@protocol PageControlDelegate;

@interface PageControl : UIView
{
@private
NSInteger _currentPage;
NSInteger _numberOfPages;
UIColor *dotColorCurrentPage;
UIColor *dotColorOtherPage;
NSObject<PageControlDelegate> *delegate;
//If ARC use __unsafe_unretained id delegate;
}

// Set these to control the PageControl.
@property (nonatomic) NSInteger currentPage;
@property (nonatomic) NSInteger numberOfPages;

// Customize these as well as the backgroundColor property.
@property (nonatomic, retain) UIColor *dotColorCurrentPage;
@property (nonatomic, retain) UIColor *dotColorOtherPage;

// Optional delegate for callbacks when user taps a page dot.
@property (nonatomic, retain) NSObject<PageControlDelegate> *delegate;

@end

@protocol PageControlDelegate<NSObject>
@optional
- (void)pageControlPageDidChange:(PageControl *)pageControl;
@end
103 changes: 103 additions & 0 deletions iCarousel/PageControl.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// PageControl.m
//
// Replacement for UIPageControl because that one only supports white dots.
//
// Created by Morten Heiberg <morten@heiberg.net> on November 1, 2010.
//

#import "PageControl.h"

// Tweak these or make them dynamic.
#define kDotDiameter 7.0
#define kDotSpacer 7.0

@implementation PageControl

@synthesize dotColorCurrentPage;
@synthesize dotColorOtherPage;
@synthesize delegate;

- (NSInteger)currentPage
{
return _currentPage;
}

- (void)setCurrentPage:(NSInteger)page
{
_currentPage = MIN(MAX(0, page), _numberOfPages-1);
[self setNeedsDisplay];
}

- (NSInteger)numberOfPages
{
return _numberOfPages;
}

- (void)setNumberOfPages:(NSInteger)pages
{
_numberOfPages = MAX(0, pages);
_currentPage = MIN(MAX(0, _currentPage), _numberOfPages-1);
[self setNeedsDisplay];
}

- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Default colors.
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.6];
self.dotColorCurrentPage = [UIColor whiteColor];
self.dotColorOtherPage = [UIColor colorWithWhite:1.000 alpha:0.4];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self addGestureRecognizer:swipeRight];




UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedLeft:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addGestureRecognizer:swipe];

}
return self;
}
-(void) swipedLeft:(UISwipeGestureRecognizer *) recognizer
{
self.currentPage++;
}
-(void) swipedRight:(UISwipeGestureRecognizer *) recognizer
{
self.currentPage--;
}

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);

CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages*kDotDiameter + MAX(0, self.numberOfPages-1)*kDotSpacer;
CGFloat x = CGRectGetMidX(currentBounds)-dotsWidth/2;
CGFloat y = CGRectGetMidY(currentBounds)-kDotDiameter/2;
for (NSInteger i=0; i<_numberOfPages; i++)
{
CGRect circleRect = CGRectMake(x, y, kDotDiameter, kDotDiameter);
if (i == _currentPage)
{
CGContextSetFillColorWithColor(context, self.dotColorCurrentPage.CGColor);
}
else
{
CGContextSetFillColorWithColor(context, self.dotColorOtherPage.CGColor);
}
CGContextFillEllipseInRect(context, circleRect);
x += kDotDiameter + kDotSpacer;
}
}

@end
4 changes: 4 additions & 0 deletions iCarousel/iCarousel.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, getter = isDecelerating) BOOL decelerating;
@property (nonatomic, readonly, getter = isScrolling) BOOL scrolling;

@property (nonatomic, assign) BOOL pageControlEnabled;
@property (nonatomic, strong) UIColor *pageControlDotColorCurrentPage;
@property (nonatomic, strong) UIColor *pageControlDotColorOtherPage;

- (void)scrollByOffset:(CGFloat)offset duration:(NSTimeInterval)duration;
- (void)scrollToOffset:(CGFloat)offset duration:(NSTimeInterval)duration;
- (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)duration;
Expand Down
31 changes: 30 additions & 1 deletion iCarousel/iCarousel.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//

#import "iCarousel.h"
#import "PageControl.h"
#import <objc/message.h>


Expand Down Expand Up @@ -121,6 +122,7 @@ @interface iCarousel ()
@property (nonatomic, assign, getter = isDragging) BOOL dragging;
@property (nonatomic, assign) BOOL didDrag;
@property (nonatomic, assign) NSTimeInterval toggleTime;
@property (nonatomic, strong) PageControl *pageControl;

NSComparisonResult compareViewDepth(UIView *view1, UIView *view2, iCarousel *self);

Expand Down Expand Up @@ -148,6 +150,10 @@ - (void)setUp
_ignorePerpendicularSwipes = YES;
_centerItemWhenSelected = YES;

_pageControlEnabled = YES;
_pageControlDotColorCurrentPage = [UIColor colorWithWhite:1 alpha:1];
_pageControlDotColorOtherPage = [UIColor colorWithWhite:1 alpha:0.4];

_contentView = [[UIView alloc] initWithFrame:self.bounds];


Expand Down Expand Up @@ -1488,6 +1494,7 @@ - (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)dura
- (void)scrollToItemAtIndex:(NSInteger)index duration:(NSTimeInterval)duration
{
[self scrollToOffset:index duration:duration];
_pageControl.currentPage = index;
}

- (void)scrollToItemAtIndex:(NSInteger)index animated:(BOOL)animated
Expand Down Expand Up @@ -1648,6 +1655,8 @@ - (void)reloadItemAtIndex:(NSInteger)index animated:(BOOL)animated

- (void)startAnimation
{
[self creatPageControl];

if (!_timer)
{
self.timer = [NSTimer timerWithTimeInterval:1.0/60.0
Expand Down Expand Up @@ -1917,7 +1926,27 @@ - (void)didScroll
//update previous index
_previousScrollOffset = _scrollOffset;
_previousItemIndex = self.currentItemIndex;
}
}


#pragma mark -
#pragma mark Page Control

- (void) creatPageControl{

if (_pageControlEnabled && !_pageControl) {

_pageControl = [PageControl new];
_pageControl.frame = CGRectMake((self.frame.size.width - (_numberOfItems * 13 + 15))/2, 10, _numberOfItems * 13 + 15, 23);
_pageControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
_pageControl.numberOfPages = _numberOfItems;
_pageControl.currentPage = 0;
_pageControl.dotColorCurrentPage = _pageControlDotColorCurrentPage;
_pageControl.dotColorOtherPage = _pageControlDotColorOtherPage;
[self addSubview:_pageControl];

}
}


#ifdef ICAROUSEL_IOS
Expand Down