[IOS]UIPageViewControl自定义
demo地址:http://download.csdn.net/detail/u012881779/7966345
#import "SMPageControl.h"
@interface HomeViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) SMPageControl *pageControl; //自定义UIPageControl
@end
@implementation WXBaseViewController
@synthesize pageControl = _pageControl;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:_headerView];
[_headerView setCenter:CGPointMake(370, 512)];
//表头初始化
NSMutableArray *tempArr = [[NSMutableArray alloc] init];
[tempArr addObject:@"1"];
[tempArr addObject:@"2"];
[tempArr addObject:@"3"];
[tempArr addObject:@"4"];
[tempArr addObject:@"5"];
[self cHeaderInfo:tempArr];
}
//初始化
-(void)cHeaderInfo:(NSMutableArray *)headerArr{
NSInteger pageNum = [headerArr count];
[self cCleanHeader];
_headerSV.delegate = self;
[_headerSV setContentSize:CGSizeMake(_headerSV.bounds.size.width*pageNum, 291)];
[_headerSV setPagingEnabled:YES];
_headerSV.tag = 3302;
[_headerSV setShowsHorizontalScrollIndicator:NO];
[_headerSV setBounces:NO];
if(headerArr){
for (int i = 0; i < [headerArr count]; i ++) {
//数据
UIImageView *tempIV = [[UIImageView alloc] init];
CGRect theRect = _headerSV.bounds;
theRect.origin.x = i*theRect.size.width;
[tempIV setFrame:theRect];
[_headerSV addSubview:tempIV];
if(i == 0){
[tempIV setBackgroundColor:[UIColor orangeColor]];
}else if(i == 1){
[tempIV setBackgroundColor:[UIColor redColor]];
}else if(i == 2){
[tempIV setBackgroundColor:[UIColor greenColor]];
}else if(i == 3){
[tempIV setBackgroundColor:[UIColor blueColor]];
}else if(i == 4){
[tempIV setBackgroundColor:[UIColor grayColor]];
}
}
}
//UIPageControl自定义
float pw = 150;
float px = (_headerSV.bounds.size.width-pw)/2;
float py = _headerSV.bounds.size.height-30;
_pageControl = [[SMPageControl alloc] initWithFrame:CGRectMake(px, py, pw, 40)];
[_headerView addSubview:_pageControl];
[_pageControl setNumberOfPages:pageNum];
_pageControl.indicatorMargin = 5.0f;//间隔
_pageControl.indicatorDiameter = 5.0f;//半径
[_pageControl setPageIndicatorImage:[UIImage imageNamed:@"图片数透明.png"]];
[_pageControl setCurrentPageIndicatorImage:[UIImage imageNamed:@"图片数.png"]];
[_pageControl setUserInteractionEnabled:YES];
//点击事件
UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cPageControl:)];
[_pageControl addGestureRecognizer:tapG];
}
//清空表头
-(void)cCleanHeader{
if(_headerSV){
NSArray *tempArr = [_headerSV subviews];
for(int i = 0; i < [tempArr count]; i ++){
UIView *tempV = (UIView *)[tempArr objectAtIndex:i];
[tempV removeFromSuperview];
tempV = nil;
}
}
}
//UIPageConntrol
-(void)cPageControl:(UITapGestureRecognizer*)gesture {
CGPoint point = [gesture locationInView:gesture.view];
SMPageControl *tempsm = _pageControl;
float the_w = tempsm.bounds.size.width/2;
if(point.x > the_w || point.x == the_w){
tempsm.currentPage += 1;
if(tempsm.currentPage > _pageControl.numberOfPages){
tempsm.currentPage = _pageControl.numberOfPages;
}
}
if(point.x < the_w){
tempsm.currentPage -= 1;
if(tempsm.currentPage < 0){
tempsm.currentPage = 0;
}
}
float vw = _headerSV.bounds.size.width;
[UIView animateWithDuration:0.3 animations:^{
[_headerSV setContentOffset:CGPointMake(vw*tempsm.currentPage, 0)];
} completion:^(BOOL finished){}];
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if(scrollView.tag == 3302){
CGPoint point = scrollView.contentOffset;
int page = point.x/scrollView.bounds.size.width;
//PageControl
[_pageControl setCurrentPage:page];
}
}