項目需求,需要一個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
初始化方法只能這一種,使用其他初始化方式的情況沒有處理完,動畫也是很簡單,后面有需要在加吧