最近看简书时,看到一个不错的自定义UIPageControl ,小兵自己也仿写了下,自定义UIPageControl,一方面系统自带的实在太多不便了,在一方面就当练习了;
这个是自定义的UIPageControl 头文件
#import <UIKit/UIKit.h>
@interface MyPageControl : UIView
typedef enum:NSInteger
{
/**
默认类型:圆形
*/
Q_PageControlStyleDefaoult = 0,
/**
正方形
*/
Q_PageControlStyleSquare,
}Q_PageControlStyle;
@property(nonatomic,assign) NSInteger Q_numberPags;
@property(nonatomic,assign) NSInteger Q_currentPag;
@property(nonatomic,strong) UIColor *Q_selectionColor;
@property(nonatomic,strong) UIColor *Q_backageColor;
@property(nonatomic,assign) Q_PageControlStyle Q_pageStyle;
- (id)initWithFrame:(CGRect)frame pageStyle:(Q_PageControlStyle)pageStyle;
@end
自定义.m文件
#import "MyPageControl.h"
#define arcColor (arc4random() % 255/256.0)
@implementation MyPageControl
- (id)initWithFrame:(CGRect)frame pageStyle:(Q_PageControlStyle)pageStyle
{
if(self = [super initWithFrame:frame])
{
/**
* 默认设置
*/
_Q_backageColor = [UIColor darkTextColor];
_Q_selectionColor = [UIColor whiteColor];
_Q_pageStyle = pageStyle;
_Q_currentPag = 0;
}
return self;
}
/**
* 设置MyPageView
*
* @param Q_numberPags
*/
- (void)setQ_numberPags:(NSInteger)Q_numberPags
{
if(_Q_numberPags != Q_numberPags)
{
_Q_numberPags = Q_numberPags;
CGFloat marger = 10;
NSLog(@"%f--",self.frame.size.width);
CGFloat width = self.frame.size.width - (Q_numberPags- 1) * marger;
CGFloat pointWidth = width/Q_numberPags;
for(int i=0;i<Q_numberPags;i++)
{
UIView *aView = [[UIView alloc] init];
aView.frame = CGRectMake((marger + pointWidth) * i, 0, pointWidth, pointWidth);
aView.backgroundColor = _Q_backageColor;
switch (_Q_pageStyle) {
case Q_PageControlStyleDefaoult:
aView.layer.cornerRadius = pointWidth/2;
aView.layer.masksToBounds = YES;
break;
case Q_PageControlStyleSquare:
break;
default:
break;
}
[self addSubview:aView];
/**
* 设置cuurrentPag
*/
if(i == 0)
{
if(_Q_selectionColor)
{
aView.backgroundColor = _Q_selectionColor;
}
else
{
aView.backgroundColor = [UIColor whiteColor];
}
}
}
}
}
/**
* 当前的currentPag
*
* @param Q_currentPag
*/
- (void)setQ_currentPag:(NSInteger)Q_currentPag
{
if(_Q_currentPag != Q_currentPag)
{
_Q_currentPag = Q_currentPag;
if(self.subviews.count)
{
for(UIView *dImg in self.subviews)
{
dImg.backgroundColor = _Q_backageColor;
}
UIView *eImg = self.subviews[_Q_currentPag];
eImg.backgroundColor = _Q_selectionColor;
}
}
}
/**
* 设置pag选中时的color
*
* @param Q_selectionColor <#Q_selectionColor description#>
*/
- (void)setQ_selectionColor:(UIColor *)Q_selectionColor
{
if(_Q_selectionColor != Q_selectionColor)
{
_Q_selectionColor = Q_selectionColor;
if(self.subviews.count)
{
UIView *aimg = self.subviews[_Q_currentPag];
aimg.backgroundColor = _Q_selectionColor;
}
}
}
/**
* 设置pag的backColor
*
* @param backgroundColor <#backgroundColor description#>
*/
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
_Q_backageColor = backgroundColor;
if(self.subviews.count != 0)
{
for(UIView *aimg in self.subviews)
{
aimg.backgroundColor = _Q_backageColor;
}
UIView *bImg = self.subviews[_Q_currentPag];
bImg.backgroundColor = _Q_selectionColor;
}
}
在UIViewController中使用
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 375, 500);
scrollView.backgroundColor = [UIColor greenColor];
scrollView.contentSize = CGSizeMake(375*5, 500);
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
for(int i=0;i<5;i++)
{
UIImageView *imageVeiw = [[UIImageView alloc] init];
imageVeiw.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d",i]];
imageVeiw.backgroundColor = [UIColor colorWithRed:arcColor green:arcColor blue:arcColor alpha:1];
imageVeiw.frame = CGRectMake(375*i, 0, 375, 450);
[scrollView addSubview:imageVeiw];
}
[self.view addSubview:scrollView];
myPag = [[MyPageControl alloc] initWithFrame:CGRectMake(130,600,100, 0) pageStyle:Q_PageControlStyleSquare];
myPag.backgroundColor = [UIColor redColor];
myPag.Q_backageColor = [UIColor cyanColor];
myPag.Q_selectionColor = [UIColor redColor];
myPag.Q_numberPags = 5;
[self.view addSubview:myPag];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollVi
{
CGFloat size = scrollView.contentOffset.x;
NSInteger index = (size/scrollView.frame.size.width) + 0.5;
NSLog(@"%f-%ld",size,index);
myPag.Q_currentPag = index;
}