// // 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 @synthesize _numberOfPages, _currentPage, hidesForSinglePage; @synthesize coreNormalColor, coreSelectedColor; @synthesize strokeNormalColor, strokeSelectedColor; @synthesize _pageControlStyle, _strokeWidth, diameter, gapWidth; @synthesize thumbImage, selectedThumbImage; #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) { // Initialization code [self setBackgroundColor:[UIColor clearColor]]; self._strokeWidth = 2; self.gapWidth = 10; self.diameter = 12; self._pageControlStyle = PageControlStyleDefault; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapped:)]; [self addGestureRecognizer:tapGestureRecognizer]; [tapGestureRecognizer release]; } return self; } - (void)onTapped:(UITapGestureRecognizer*)gesture { CGPoint touchPoint = [gesture locationInView:[gesture view]]; if (touchPoint.x < self.frame.size.width/2) { // move left if (self._currentPage>0) { self._currentPage -= 1; } } else { // move right if (self._currentPageself.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