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

Image support (onImage offImage). #9

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions Classes/DDPageControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ DDPageControlType ;
@property (nonatomic) CGFloat indicatorDiameter ;
@property (nonatomic) CGFloat indicatorSpace ;

@property (nonatomic, retain) NSString *onImage ;
@property (nonatomic, retain) NSString *offImage ;

@end

99 changes: 59 additions & 40 deletions Classes/DDPageControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ @implementation DDPageControl
@synthesize offColor ;
@synthesize indicatorDiameter ;
@synthesize indicatorSpace ;
@synthesize onImage ;
@synthesize offImage ;

#pragma mark -
#pragma mark Initializers - dealloc
Expand Down Expand Up @@ -55,7 +57,9 @@ - (void)dealloc
{
[onColor release], onColor = nil ;
[offColor release], offColor = nil ;

[onImage release], onImage = nil;
[offImage release], offImage = nil;

[super dealloc] ;
}

Expand Down Expand Up @@ -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) ;
}


Expand Down