diff --git a/Classes/DDPageControl.m b/Classes/DDPageControl.m index 769e45e..6195b67 100644 --- a/Classes/DDPageControl.m +++ b/Classes/DDPageControl.m @@ -15,48 +15,48 @@ @implementation DDPageControl -@synthesize numberOfPages ; -@synthesize currentPage ; -@synthesize hidesForSinglePage ; -@synthesize defersCurrentPageDisplay ; +@synthesize numberOfPages; +@synthesize currentPage; +@synthesize hidesForSinglePage; +@synthesize defersCurrentPageDisplay; -@synthesize type ; -@synthesize onColor ; -@synthesize offColor ; -@synthesize indicatorDiameter ; -@synthesize indicatorSpace ; +@synthesize type; +@synthesize onColor; +@synthesize offColor; +@synthesize indicatorDiameter; +@synthesize indicatorSpace; #pragma mark - #pragma mark Initializers - dealloc - (id)initWithType:(DDPageControlType)theType { - self = [self initWithFrame: CGRectZero] ; - [self setType: theType] ; - return self ; + self = [self initWithFrame:CGRectZero]; + [self setType:theType]; + return self; } - (id)init { - self = [self initWithFrame: CGRectZero] ; - return self ; + self = [self initWithFrame:CGRectZero]; + return self; } - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { - self.backgroundColor = [UIColor clearColor] ; + self.backgroundColor = [UIColor clearColor]; } - return self ; + return self; } - (void)dealloc { - [onColor release], onColor = nil ; - [offColor release], offColor = nil ; - - [super dealloc] ; + [onColor release], onColor = nil; + [offColor release], offColor = nil; + + [super dealloc]; } @@ -66,65 +66,65 @@ - (void)dealloc - (void)drawRect:(CGRect)rect { // get the current context - CGContextRef context = UIGraphicsGetCurrentContext() ; - + CGContextRef context = UIGraphicsGetCurrentContext(); + // save the context - CGContextSaveGState(context) ; - + CGContextSaveGState(context); + // allow antialiasing - CGContextSetAllowsAntialiasing(context, TRUE) ; - + CGContextSetAllowsAntialiasing(context, TRUE); + // get the caller's diameter if it has been set or use the default one - CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter ; - CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace ; - + CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter; + CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace; + // geometry - CGRect currentBounds = self.bounds ; - CGFloat dotsWidth = self.numberOfPages * diameter + MAX(0, self.numberOfPages - 1) * space ; - CGFloat x = CGRectGetMidX(currentBounds) - dotsWidth / 2 ; - CGFloat y = CGRectGetMidY(currentBounds) - diameter / 2 ; - + CGRect currentBounds = self.bounds; + 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]; - + UIColor *drawOnColor = onColor ? onColor : [UIColor colorWithWhite:1.0f alpha:1.0f]; + UIColor *drawOffColor = offColor ? offColor : [UIColor colorWithWhite:0.7f alpha:0.5f]; + // actually draw the dots - for (int i = 0 ; i < numberOfPages ; i++) + for (int i = 0; i < numberOfPages; i++) { - CGRect dotRect = CGRectMake(x, y, diameter, diameter) ; - + 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)) ; + CGContextSetFillColorWithColor(context, drawOnColor.CGColor); + CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)); } else { - CGContextSetStrokeColorWithColor(context, drawOnColor.CGColor) ; - CGContextStrokeEllipseInRect(context, dotRect) ; + CGContextSetStrokeColorWithColor(context, drawOnColor.CGColor); + CGContextStrokeEllipseInRect(context, dotRect); } } else { if (type == DDPageControlTypeOnEmptyOffEmpty || type == DDPageControlTypeOnFullOffEmpty) { - CGContextSetStrokeColorWithColor(context, drawOffColor.CGColor) ; - CGContextStrokeEllipseInRect(context, dotRect) ; + CGContextSetStrokeColorWithColor(context, drawOffColor.CGColor); + CGContextStrokeEllipseInRect(context, dotRect); } else { - CGContextSetFillColorWithColor(context, drawOffColor.CGColor) ; - CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ; + CGContextSetFillColorWithColor(context, drawOffColor.CGColor); + CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)); } } - - x += diameter + space ; + + x += diameter + space; } - + // restore the context - CGContextRestoreGState(context) ; + CGContextRestoreGState(context); } @@ -135,108 +135,108 @@ - (void)setCurrentPage:(NSInteger)pageNumber { // no need to update in that case if (currentPage == pageNumber) - return ; - + return; + // determine if the page number is in the available range - currentPage = MIN(MAX(0, pageNumber), numberOfPages - 1) ; - + currentPage = MIN(MAX(0, pageNumber), numberOfPages - 1); + // in case we do not defer the page update, we redraw the view if (self.defersCurrentPageDisplay == NO) - [self setNeedsDisplay] ; + [self setNeedsDisplay]; } - (void)setNumberOfPages:(NSInteger)numOfPages { // make sure the number of pages is positive - numberOfPages = MAX(0, numOfPages) ; - + numberOfPages = MAX(0, numOfPages); + // we then need to update the current page - currentPage = MIN(MAX(0, currentPage), numberOfPages - 1) ; - + currentPage = MIN(MAX(0, currentPage), numberOfPages - 1); + // correct the bounds accordingly - self.bounds = self.bounds ; - + self.bounds = self.bounds; + // we need to redraw - [self setNeedsDisplay] ; - + [self setNeedsDisplay]; + // depending on the user preferences, we hide the page control with a single element if (hidesForSinglePage && (numOfPages < 2)) - [self setHidden: YES] ; + [self setHidden:YES]; else - [self setHidden: NO] ; + [self setHidden:NO]; } - (void)setHidesForSinglePage:(BOOL)hide { - hidesForSinglePage = hide ; - + hidesForSinglePage = hide; + // depending on the user preferences, we hide the page control with a single element if (hidesForSinglePage && (numberOfPages < 2)) - [self setHidden: YES] ; + [self setHidden:YES]; } - (void)setDefersCurrentPageDisplay:(BOOL)defers { - defersCurrentPageDisplay = defers ; + defersCurrentPageDisplay = defers; } - (void)setType:(DDPageControlType)aType { - type = aType ; - - [self setNeedsDisplay] ; + type = aType; + + [self setNeedsDisplay]; } - (void)setOnColor:(UIColor *)aColor { - [aColor retain] ; - [onColor release] ; - onColor = aColor ; - - [self setNeedsDisplay] ; + [aColor retain]; + [onColor release]; + onColor = aColor; + + [self setNeedsDisplay]; } - (void)setOffColor:(UIColor *)aColor { - [aColor retain] ; - [offColor release] ; - offColor = aColor ; - - [self setNeedsDisplay] ; + [aColor retain]; + [offColor release]; + offColor = aColor; + + [self setNeedsDisplay]; } - (void)setIndicatorDiameter:(CGFloat)aDiameter { - indicatorDiameter = aDiameter ; - + indicatorDiameter = aDiameter; + // correct the bounds accordingly - self.bounds = self.bounds ; - - [self setNeedsDisplay] ; + self.bounds = self.bounds; + + [self setNeedsDisplay]; } - (void)setIndicatorSpace:(CGFloat)aSpace { - indicatorSpace = aSpace ; - + indicatorSpace = aSpace; + // correct the bounds accordingly - self.bounds = self.bounds ; - - [self setNeedsDisplay] ; + self.bounds = self.bounds; + + [self setNeedsDisplay]; } /*- (void)setFrame:(CGRect)aFrame { // we do not allow the caller to modify the size struct in the frame so we compute it - aFrame.size = [self sizeForNumberOfPages: numberOfPages] ; - super.frame = aFrame ; + aFrame.size = [self sizeForNumberOfPages:numberOfPages]; + super.frame = aFrame; } - (void)setBounds:(CGRect)aBounds { // we do not allow the caller to modify the size struct in the bounds so we compute it - aBounds.size = [self sizeForNumberOfPages: numberOfPages] ; - super.bounds = aBounds ; + aBounds.size = [self sizeForNumberOfPages:numberOfPages]; + super.bounds = aBounds; }*/ @@ -248,18 +248,18 @@ - (void)updateCurrentPageDisplay { // ignores this method if the value of defersPageIndicatorUpdate is NO if (self.defersCurrentPageDisplay == NO) - return ; - + return; + // in case it is YES, we redraw the view (that will update the page control to the correct page) - [self setNeedsDisplay] ; + [self setNeedsDisplay]; } - (CGSize)sizeForNumberOfPages:(NSInteger)pageCount { - CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter ; - CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace ; - - return CGSizeMake(pageCount * diameter + (pageCount - 1) * space + 44.0f, MAX(44.0f, diameter + 4.0f)) ; + CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter; + CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace; + + return CGSizeMake(pageCount * diameter + (pageCount - 1) * space + 44.0f, MAX(44.0f, diameter + 4.0f)); } @@ -269,17 +269,17 @@ - (CGSize)sizeForNumberOfPages:(NSInteger)pageCount - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // get the touch location - UITouch *theTouch = [touches anyObject] ; - CGPoint touchLocation = [theTouch locationInView: self] ; - + UITouch *theTouch = [touches anyObject]; + CGPoint touchLocation = [theTouch locationInView:self]; + // check whether the touch is in the right or left hand-side of the control if (touchLocation.x < (self.bounds.size.width / 2)) - self.currentPage = MAX(self.currentPage - 1, 0) ; + self.currentPage = MAX(self.currentPage - 1, 0); else - self.currentPage = MIN(self.currentPage + 1, numberOfPages - 1) ; - + self.currentPage = MIN(self.currentPage + 1, numberOfPages - 1); + // send the value changed action to the target - [self sendActionsForControlEvents: UIControlEventValueChanged] ; + [self sendActionsForControlEvents:UIControlEventValueChanged]; } -@end \ No newline at end of file +@end