// // PageControl.m // PageControlDemo // /** * Created by honcheng on 5/14/11. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject * to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR * IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @author Muh Hon Cheng http://twitter.com/honcheng * @copyright 2011 Muh Hon Cheng * */ #import "StyledPageControl.h" @implementation StyledPageControl #define COLOR_GRAYISHBLUE [UIColor colorWithRed:128/255.0 green:130/255.0 blue:133/255.0 alpha:1] #define COLOR_GRAY [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1] - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } -(id)initWithCoder:(NSCoder *)aDecoder{ self=[super initWithCoder:aDecoder]; if (self) { [self setup]; } return self; } -(void)setup{ [self setBackgroundColor:[UIColor clearColor]]; _strokeWidth = 2; _gapWidth = 10; _diameter = 12; _pageControlStyle = PageControlStyleDefault; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapped:)]; [self addGestureRecognizer:tapGestureRecognizer]; } - (void)onTapped:(UITapGestureRecognizer*)gesture { CGPoint touchPoint = [gesture locationInView:[gesture view]]; if (touchPoint.x < self.frame.size.width/2) { // move left if (self.currentPage>0) { if (touchPoint.x <= 22) { self.currentPage = 0; } else { self.currentPage -= 1; } } } else { // move right if (self.currentPage= (CGRectGetWidth(self.bounds) - 22)) { self.currentPage = self.numberOfPages-1; } else { self.currentPage += 1; } } } [self setNeedsDisplay]; [self sendActionsForControlEvents:UIControlEventValueChanged]; } - (void)drawRect:(CGRect)rect { UIColor *coreNormalColor, *coreSelectedColor, *strokeNormalColor, *strokeSelectedColor; if (self.coreNormalColor) coreNormalColor = self.coreNormalColor; else coreNormalColor = COLOR_GRAYISHBLUE; if (self.coreSelectedColor) coreSelectedColor = self.coreSelectedColor; else { if (self.pageControlStyle==PageControlStyleStrokedSquare || self.pageControlStyle==PageControlStyleStrokedCircle || self.pageControlStyle==PageControlStyleWithPageNumber) { coreSelectedColor = COLOR_GRAYISHBLUE; } else { coreSelectedColor = COLOR_GRAY; } } if (self.strokeNormalColor) strokeNormalColor = self.strokeNormalColor; else { if (self.pageControlStyle==PageControlStyleDefault && self.coreNormalColor) { strokeNormalColor = self.coreNormalColor; } else { strokeNormalColor = COLOR_GRAYISHBLUE; } } if (self.strokeSelectedColor) strokeSelectedColor = self.strokeSelectedColor; else { if (self.pageControlStyle==PageControlStyleStrokedSquare || self.pageControlStyle==PageControlStyleStrokedCircle || self.pageControlStyle==PageControlStyleWithPageNumber) { strokeSelectedColor = COLOR_GRAYISHBLUE; } else if (self.pageControlStyle==PageControlStyleDefault && self.coreSelectedColor) { strokeSelectedColor = self.coreSelectedColor; } else { strokeSelectedColor = COLOR_GRAY; } } // Drawing code if (self.hidesForSinglePage && self.numberOfPages==1) { return; } CGContextRef myContext = UIGraphicsGetCurrentContext(); int gap = self.gapWidth; float diameter = self.diameter - 2*self.strokeWidth; if (self.pageControlStyle==PageControlStyleThumb) { if (self.thumbImage && self.selectedThumbImage) { diameter = self.thumbImage.size.width; } } int total_width = self.numberOfPages*diameter + (self.numberOfPages-1)*gap; if (total_width>self.frame.size.width) { while (total_width>self.frame.size.width) { diameter -= 2; gap = diameter + 2; while (total_width>self.frame.size.width) { gap -= 1; total_width = self.numberOfPages*diameter + (self.numberOfPages-1)*gap; if (gap==2) { break; total_width = self.numberOfPages*diameter + (self.numberOfPages-1)*gap; } } if (diameter==2) { break; total_width = self.numberOfPages*diameter + (self.numberOfPages-1)*gap; } } } int i; for (i=0; i