-
有時(shí)候我們根據(jù)項(xiàng)目開發(fā)的需要,系統(tǒng)為的UIPageControl的樣式可能并不是我們想要的焊夸,這個(gè)時(shí)候我們就需要根據(jù)自己實(shí)際需求情況自定義UIPageControl
例如:
自定義方式1
自定義方式2
- 那么這種效果怎么實(shí)現(xiàn)呢?別的不多說蓝角,我們上代碼代碼:首先:我們自定義一個(gè)類:CustomPageControl類 其父類是UIView
.h
#import <UIKit/UIKit.h>
@interface CustomPageControl : UIView
typedef enum: NSInteger
{
//默認(rèn)類型/圓形
PageControlStyleCircle = 0,
//正方形
PageControlStyleSquare,
//......
}PageControlStyle;
@property(nonatomic,assign)NSInteger numberOfPags; //點(diǎn)個(gè)數(shù)
@property(nonatomic,assign)NSInteger currentPags; //當(dāng)前點(diǎn)位置
@property(nonatomic,strong)UIColor *selectedColor; //選中的色
@property(nonatomic,strong)UIColor *backPageColor; //背景色
@property(nonatomic,assign)PageControlStyle pageControlStyle;//類別
//自定義初始化方法
-(id)initWithFrame:(CGRect)frame pageControlStyle:(PageControlStyle)pageControlStyle ;
@end
.m
初始化
-(id)initWithFrame:(CGRect)frame pageControlStyle:(PageControlStyle)pageControlStyle{
if (self = [super initWithFrame:frame]) {
//默認(rèn)設(shè)置
_backPageColor = [UIColor darkTextColor];
_selectedColor = [UIColor whiteColor];
_pageControlStyle = pageControlStyle;
_currentPags = 0;
}
return self;
}
設(shè)置PageView
-(void)setNumberOfPags:(NSInteger)numberOfPags{
if (_numberOfPags != numberOfPags) {
_numberOfPags = numberOfPags;
CGFloat margin = 10;
NSLog(@"%f",self.frame.size.width);
CGFloat width = self.frame.size.width - (numberOfPags - 1)*margin;
CGFloat pointWidth = width / numberOfPags;
for (int i = 0; i < numberOfPags; i++) {
UIView *aview = [[UIView alloc]init];
aview.frame = CGRectMake((margin + pointWidth) * i, 0, pointWidth, pointWidth);
aview.backgroundColor = _backPageColor;
switch (_pageControlStyle) {
case PageControlStyleCircle:
aview.layer.cornerRadius = pointWidth / 2 ;
aview.layer.masksToBounds = YES;
break;
case PageControlStyleSquare:
break;
default:
break;
}
[self addSubview:aview];
/**
* 設(shè)置cuurrentPag
*/
if (i == 0) {
if (_selectedColor) {
aview.backgroundColor = _selectedColor;
}
else
{
aview.backgroundColor = [UIColor whiteColor];
}
}
}
}
}
- 當(dāng)前的currentPage
-(void)setCurrentPags:(NSInteger)currentPags{
if (_currentPags != currentPags) {
_currentPags = currentPags;
if (self.subviews.count) {
for (UIView *aView in self.subviews) {
aView.backgroundColor = _backPageColor;
}
UIView *aView = self.subviews[_currentPags];
aView.backgroundColor = _selectedColor;
}
}
}
- 設(shè)置選中的顏色
-(void)setSelectedColor:(UIColor *)selectedColor{
if (_selectedColor != selectedColor) {
_selectedColor = selectedColor;
if (self.subviews.count) {
UIView *aView = self.subviews[_currentPags];
aView.backgroundColor = _selectedColor;
}
}
}
- 設(shè)置背景色
-(void)setbackgroundColor:(UIColor *)backgroundColor{
if (_backPageColor != backgroundColor) {
_backPageColor = backgroundColor;
if (self.subviews.count != 0) {
for (UIView *aView in self.subviews) {
aView.backgroundColor = _backPageColor;
}
UIView *aView = self.subviews[_currentPags];
aView.backgroundColor = _selectedColor;
}
}
}
- 以上是對(duì)UIPageControl的自定義 那么使用起來就更簡單了我們導(dǎo)入頭文件
#import "CustomPageControl.h"
@interface ViewController ()<UIScrollViewDelegate>
{
CustomPageControl *pageControl;
UIScrollView *scrollView;
}
@end
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
- 在viewDidLoad 中:
scrollView = [[UIScrollView alloc]init];
scrollView.frame= CGRectMake(0, 0, screenWidth, screenHeight);
scrollView.contentSize = CGSizeMake(5 * screenWidth, screenHeight);
scrollView.delegate =self;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
[self.view addSubview:scrollView];
for (int i = 1; i <= 5; i++) {
UIImageView *aImageView = [[UIImageView alloc]init];
aImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
aImageView.frame = CGRectMake((i - 1) * screenWidth, 0, screenWidth, screenHeight);
aImageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:aImageView];
}
pageControl = [[CustomPageControl alloc]initWithFrame:CGRectMake(100, screenHeight - 50, screenWidth - 200, 35) pageControlStyle:PageControlStyleSquare];
// pageControl.backgroundColor = [UIColor redColor];
pageControl.backPageColor = [UIColor cyanColor];
pageControl.selectedColor = [UIColor blackColor];
pageControl.numberOfPags = 5;
[self.view addSubview:pageControl];
- 由于
scrollView.delegate =self;
我們實(shí)現(xiàn)UIScrollViewDelegate中的scrollViewDidScroll:
方法 用來監(jiān)聽在scrollView滾動(dòng)的時(shí)候的偏移量 來改變pageControl的顯示樣式:
- (void)scrollViewDidScroll:(UIScrollView *)scrollVi
{
CGFloat size = scrollView.contentOffset.x;
NSInteger index = (size/scrollView.frame.size.width) + 0.5;
NSLog(@"%f-%ld",size,index);
pageControl.currentPags = index;
}
- 此時(shí) 運(yùn)行程序 就是我們想要看到的效果了阱穗。