UIPageControl的自定義

  • 有時候我們根據(jù)項目開發(fā)的需要,系統(tǒng)為的UIPageControl的樣式可能并不是我們想要的,這個時候我們就需要根據(jù)自己實際需求情況自定義UIPageControl
    例如:


    自定義方式1
自定義方式2
  • 那么這種效果怎么實現(xiàn)呢?別的不多說,我們上代碼代碼:首先:我們自定義一個類:CustomPageControl類 其父類是UIView

.h

#import <UIKit/UIKit.h>
@interface CustomPageControl : UIView

typedef enum: NSInteger
{
    //默認(rèn)類型/圓形
    PageControlStyleCircle = 0,
    //正方形
    PageControlStyleSquare,
    
    //......
}PageControlStyle;

@property(nonatomic,assign)NSInteger numberOfPags; //點個數(shù)
@property(nonatomic,assign)NSInteger currentPags;  //當(dāng)前點位置

@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;
        }
    }
}
  • 以上是對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;我們實現(xiàn)UIScrollViewDelegate中的scrollViewDidScroll:方法 用來監(jiān)聽在scrollView滾動的時候的偏移量 來改變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;
}
  • 此時 運行程序 就是我們想要看到的效果了。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,711評論 1 14
  • 根據(jù)項目開發(fā)的需要,系統(tǒng)為的UIPageControl的樣式可能并不是我們想要的,這個時候我們就需要根據(jù)自己實際需...
    見哥哥長高了閱讀 971評論 0 0
  • 01 一切都是為了更好的分享和溝通 兩年前我參加CityLab研討會,與一群關(guān)注城市未來的職業(yè)建筑師、學(xué)生、企業(yè)家...
    布拉格向北閱讀 1,011評論 1 15
  • 第二章:家的感覺 上一章 飛機(jī)上,小米蓋著毯子,舒服地躺在靠窗的座位上,盯著窗外發(fā)呆。 她還沒有告訴媽媽自己回國的...
    鹽果兒閱讀 427評論 0 1

友情鏈接更多精彩內(nèi)容