diff --git a/iCarousel/PageControl.h b/iCarousel/PageControl.h new file mode 100644 index 0000000000..bee0c5741e --- /dev/null +++ b/iCarousel/PageControl.h @@ -0,0 +1,40 @@ +// +// PageControl.h +// +// Replacement for UIPageControl because that one only supports white dots. +// +// Created by Morten Heiberg on November 1, 2010. +// + +#import + +@protocol PageControlDelegate; + +@interface PageControl : UIView +{ +@private + NSInteger _currentPage; + NSInteger _numberOfPages; + UIColor *dotColorCurrentPage; + UIColor *dotColorOtherPage; + NSObject *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 *delegate; + +@end + +@protocol PageControlDelegate +@optional +- (void)pageControlPageDidChange:(PageControl *)pageControl; +@end \ No newline at end of file diff --git a/iCarousel/PageControl.m b/iCarousel/PageControl.m new file mode 100644 index 0000000000..0ffb35596a --- /dev/null +++ b/iCarousel/PageControl.m @@ -0,0 +1,103 @@ +// +// PageControl.m +// +// Replacement for UIPageControl because that one only supports white dots. +// +// Created by Morten Heiberg 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 \ No newline at end of file diff --git a/iCarousel/iCarousel.h b/iCarousel/iCarousel.h index d49c268203..27b203f794 100644 --- a/iCarousel/iCarousel.h +++ b/iCarousel/iCarousel.h @@ -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; diff --git a/iCarousel/iCarousel.m b/iCarousel/iCarousel.m index 48a6f32f74..a88a90e5e0 100644 --- a/iCarousel/iCarousel.m +++ b/iCarousel/iCarousel.m @@ -31,6 +31,7 @@ // #import "iCarousel.h" +#import "PageControl.h" #import @@ -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); @@ -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]; @@ -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 @@ -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 @@ -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