项目需求,需要一个PageControl,dot里是1,2,3,4数字的,随便写了一个,有一些坑没填完,用的时候注意就没什么问题。
#import <UIKit/UIKit.h>
@interface CZYPageControl : UIView
//pageDot之间的间隔
@property(nonatomic,assign)float spacing;
//未选中的dot尺寸
@property(nonatomic,assign)float minDotValue;
//选中的dot尺寸
@property(nonatomic,assign)float maxDotValue;
//未选中dot的背景颜色
@property(nonatomic,strong)UIColor *NoSelectedDotBackColor;
//选中dot的背景颜色
@property(nonatomic,strong)UIColor *SelectedDotBackColor;
//dot中间字体的颜色
@property(nonatomic,strong)UIColor *fontColor;
//scrol是所关联的那个UIScrollView
-(instancetype)CpageInitWithFrame:(CGRect)frame pageNumber:(NSInteger)pageNumber controllingScrol:(UIScrollView *)scrol;
@end
#import "CZYPageControl.h"
@interface CZYPageControl()
@property(nonatomic,assign)NSInteger pageNumber;
@property(nonatomic,strong)UIScrollView *scrol;
@end
@implementation CZYPageControl
-(instancetype)CpageInitWithFrame:(CGRect)frame pageNumber:(NSInteger)pageNumber controllingScrol:(UIScrollView *)scrol
{
if ([super initWithFrame:frame]) {
_pageNumber=pageNumber;
_scrol=scrol;
[self setDefault];
}
return self;
}
//默认设置
-(void)setDefault
{
_spacing=11.0;
_minDotValue=22.0;
_maxDotValue=32.0;
_NoSelectedDotBackColor=[UIColor grayColor];
_SelectedDotBackColor=GREEN;
_fontColor=[UIColor whiteColor];
[_scrol addObserver:self forKeyPath:@"contentOffset" options:(NSKeyValueObservingOptionNew) context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
CGPoint newOffset = [change[@"new"] CGPointValue];
NSLog(@"%lf",newOffset.x);
int index=(newOffset.x) / ([UIScreen mainScreen].bounds.size.width);
for (int i=0; i<_pageNumber; i++) {
[self viewWithTag:10+i].hidden=YES;
}
[self viewWithTag:10+index].hidden=NO;
while (newOffset.x>WIDTH) {
newOffset.x-=WIDTH;
}
}
-(void)layoutSubviews
{
[self creatUI];
[_scrol setContentOffset:CGPointMake(0, 0)];
}
-(void)creatUI
{
for (int i=0; i<_pageNumber; i++) {
UILabel *dotLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/2-((_pageNumber*_minDotValue+(_pageNumber-1)*_spacing)/2)+i*(_minDotValue+_spacing) , self.frame.size.height/2-_minDotValue/2, _minDotValue, _minDotValue)];
dotLabel.tag=i;
dotLabel.layer.cornerRadius=_minDotValue/2;
dotLabel.layer.masksToBounds=YES;
dotLabel.backgroundColor=_NoSelectedDotBackColor;
dotLabel.font=[UIFont fontWithName:@"Lato-Regular" size:13];
dotLabel.textAlignment=NSTextAlignmentCenter;
dotLabel.textColor=_fontColor;
dotLabel.text=[NSString stringWithFormat:@"%d",i+1];
[self addSubview:dotLabel];
UILabel *selectdotLabel=[[UILabel alloc]init];
selectdotLabel.center=CGPointMake(dotLabel.center.x, dotLabel.center.y);
selectdotLabel.bounds=CGRectMake(0, 0, _maxDotValue, _maxDotValue);
selectdotLabel.tag=i+10;
selectdotLabel.layer.cornerRadius=_maxDotValue/2;
selectdotLabel.layer.masksToBounds=YES;
selectdotLabel.backgroundColor=_SelectedDotBackColor;
selectdotLabel.font=[UIFont fontWithName:@"Lato-Regular" size:13];
selectdotLabel.textAlignment=NSTextAlignmentCenter;
selectdotLabel.textColor=_fontColor;
selectdotLabel.text=[NSString stringWithFormat:@"%d",i+1];
[self addSubview:selectdotLabel];
selectdotLabel.hidden=YES;
}
}
-(void)dealloc
{
[_scrol removeObserver:self forKeyPath:@"contentOffset"];
}
@end
初始化方法只能这一种,使用其他初始化方式的情况没有处理完,动画也是很简单,后面有需要在加吧
网友评论