2011年5月3日 星期二

自定義UIPageControl的介面

今天工作時發現要修改UIPageControl的介面,
不過上Google找不到想要的答案,
只好自己試出來了XD

其實修改2個Function就可以做到
- (void)setNumberOfPages:(NSInteger)pages;
- (void)setCurrentPage:(NSInteger)page;

- (void)setNumberOfPages:(NSInteger)pages
{
// super class function
[super setNumberOfPages:pages];

// search all page image view
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* imageView = [self.subviews objectAtIndex:i];
[imageView setFrame:CGRectMake(0, 0, 25, 25)];
// if image view no subview
if([imageView.subviews count] == 0)
{
// add a new subview cover image view
UILabel* numLabel = [[UILabel alloc] initWithFrame:imageView.frame];
[numLabel setText:[NSString stringWithFormat:@"%d", i + 1]];
[numLabel setBackgroundColor:[UIColor blackColor]];
[numLabel setTextAlignment:UITextAlignmentCenter];
[numLabel setTextColor:[UIColor whiteColor]];
[imageView addSubview:numLabel];
[numLabel release];
}

// edit the page image view each position
[imageView setCenter:CGPointMake(40 * i, self.frame.size.height / 2)];
imageView.layer.borderWidth = 2;
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.cornerRadius = 8;
imageView.layer.masksToBounds = TRUE;
}
}

- (void)setCurrentPage:(NSInteger)page
{
// super class function
[super setCurrentPage:page];

// clean all image view select state
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* imageView = [self.subviews objectAtIndex:i];
UILabel* numLabel = [imageView.subviews objectAtIndex:0];
[numLabel setBackgroundColor:[UIColor blackColor]];
[numLabel setTextColor:[UIColor whiteColor]];
}

// update the current select page image view state
UIImageView* imageView = [self.subviews objectAtIndex:page];
UILabel* numLabel = [imageView.subviews objectAtIndex:0];
[numLabel setBackgroundColor:[UIColor whiteColor]];
[numLabel setTextColor:[UIColor blackColor]];
}

沒有留言:

張貼留言