From a981941529622bee6a1f1dea39ce2e302fbdc5cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9raphin=20Huguenot?= Date: Fri, 24 Aug 2012 22:56:09 +0200 Subject: [PATCH] Image support (onImage offImage). --- Classes/DDPageControl.h | 3 ++ Classes/DDPageControl.m | 99 ++++++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 40 deletions(-) diff --git a/Classes/DDPageControl.h b/Classes/DDPageControl.h index 9a0317c..6c494ef 100644 --- a/Classes/DDPageControl.h +++ b/Classes/DDPageControl.h @@ -51,5 +51,8 @@ DDPageControlType ; @property (nonatomic) CGFloat indicatorDiameter ; @property (nonatomic) CGFloat indicatorSpace ; +@property (nonatomic, retain) NSString *onImage ; +@property (nonatomic, retain) NSString *offImage ; + @end diff --git a/Classes/DDPageControl.m b/Classes/DDPageControl.m index 53111d2..8823679 100644 --- a/Classes/DDPageControl.m +++ b/Classes/DDPageControl.m @@ -25,6 +25,8 @@ @implementation DDPageControl @synthesize offColor ; @synthesize indicatorDiameter ; @synthesize indicatorSpace ; +@synthesize onImage ; +@synthesize offImage ; #pragma mark - #pragma mark Initializers - dealloc @@ -55,7 +57,9 @@ - (void)dealloc { [onColor release], onColor = nil ; [offColor release], offColor = nil ; - + [onImage release], onImage = nil; + [offImage release], offImage = nil; + [super dealloc] ; } @@ -83,48 +87,63 @@ - (void)drawRect:(CGRect)rect CGFloat dotsWidth = self.numberOfPages * diameter + MAX(0, self.numberOfPages - 1) * space ; CGFloat x = CGRectGetMidX(currentBounds) - dotsWidth / 2 ; CGFloat y = CGRectGetMidY(currentBounds) - diameter / 2 ; - - // get the caller's colors it they have been set or use the defaults - UIColor *drawOnColor = onColor ? onColor : [UIColor colorWithWhite: 1.0f alpha: 1.0f]; - UIColor *drawOffColor = offColor ? offColor : [UIColor colorWithWhite: 0.7f alpha: 0.5f]; - + + BOOL useImages = (onImage.length || offImage.length); + // actually draw the dots for (int i = 0 ; i < numberOfPages ; i++) { - CGRect dotRect = CGRectMake(x, y, diameter, diameter) ; - - if (i == currentPage) - { - if (type == DDPageControlTypeOnFullOffFull || type == DDPageControlTypeOnFullOffEmpty) - { - CGContextSetFillColorWithColor(context, drawOnColor.CGColor) ; - CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ; - } - else - { - CGContextSetStrokeColorWithColor(context, drawOnColor.CGColor) ; - CGContextStrokeEllipseInRect(context, dotRect) ; - } - } - else - { - if (type == DDPageControlTypeOnEmptyOffEmpty || type == DDPageControlTypeOnFullOffEmpty) - { - CGContextSetStrokeColorWithColor(context, drawOffColor.CGColor) ; - CGContextStrokeEllipseInRect(context, dotRect) ; - } - else - { - CGContextSetFillColorWithColor(context, drawOffColor.CGColor) ; - CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ; - } - } - - x += diameter + space ; - } - - // restore the context - CGContextRestoreGState(context) ; + if (useImages) + { + NSString *imageName = (i == currentPage) ? self.onImage : self.offImage; + UIImage *image = [UIImage imageNamed:imageName]; + + CGFloat height = (diameter * image.size.height) / image.size.width; + CGRect dotRect = CGRectMake(x, y, diameter, height) ; + + [image drawInRect:dotRect]; + } + else + { + // get the caller's colors it they have been set or use the defaults + UIColor *drawOnColor = onColor ? onColor : [UIColor colorWithWhite: 1.0f alpha: 1.0f]; + UIColor *drawOffColor = offColor ? offColor : [UIColor colorWithWhite: 0.7f alpha: 0.5f]; + + CGRect dotRect = CGRectMake(x, y, diameter, diameter) ; + + if (i == currentPage) + { + if (type == DDPageControlTypeOnFullOffFull || type == DDPageControlTypeOnFullOffEmpty) + { + CGContextSetFillColorWithColor(context, drawOnColor.CGColor) ; + CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ; + } + else + { + CGContextSetStrokeColorWithColor(context, drawOnColor.CGColor) ; + CGContextStrokeEllipseInRect(context, dotRect) ; + } + } + else + { + if (type == DDPageControlTypeOnEmptyOffEmpty || type == DDPageControlTypeOnFullOffEmpty) + { + CGContextSetStrokeColorWithColor(context, drawOffColor.CGColor) ; + CGContextStrokeEllipseInRect(context, dotRect) ; + } + else + { + CGContextSetFillColorWithColor(context, drawOffColor.CGColor) ; + CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ; + } + } + } + + x += diameter + space ; + } + + // restore the context + CGContextRestoreGState(context) ; }