自定義UIPageControl

借鑒文章:http://blog.csdn.net/showhilllee/article/details/8350663
找了好多文章,想找個(gè)最簡(jiǎn)單的方法來(lái)自定義pageControl,覺(jué)得這種方法是代碼相對(duì)較少的,但是帖子比較古老了,里面的 updateDots 設(shè)置的為UImageView,現(xiàn)在xcode是跑不起來(lái)的,dug下發(fā)現(xiàn)原來(lái),現(xiàn)在的subviews 中存的為UIView,直接上一段代碼,隨手寫的demo沒(méi)有像外界提供任何接口,純屬了解思路用

.h文件中

import <UIKit/UIKit.h>

@interface XMPageControl : UIPageControl

@end

.m文件

import "XMPageControl.h"

#define SCREEN_WITH [[UIScreen mainScreen]bounds].size.width

@implementation XMPageControl

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    return self;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    [super endTrackingWithTouch:touch withEvent:event];
    [self updateDots];
}

- (void)updateDots {
    for (int i=0; i<self.subviews.count; ++i) {
        UIView *dot = [self.subviews objectAtIndex:i];
        //此處兩個(gè)color可以抽出,為外界提供接口
        dot.backgroundColor = i==self.currentPage ? [UIColor orangeColor]:[UIColor redColor];
    }
}

/**
 *  如果想實(shí)現(xiàn)帖子這種http://blog.csdn.net/showhilllee/article/details/8350663設(shè)置image,
 *  可參考思路,在layoutSurviews中 remove掉所有的 view 替換為imageView,
 *  還可設(shè)置uiview的layer根據(jù)image繪制 backgroundcolor
 */

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat space = 5.f;    //space, dotW H 等可以可抽出為外界所用
    CGFloat dotW = SCREEN_WITH/14.f;
    CGFloat dotH = dotW/7.f;
    CGFloat totalWith = (dotW+space)  * self.numberOfPages;
    for (int i=0; i<self.subviews.count; ++i) {
        UIView *dot = [self.subviews objectAtIndex:i];
        CGFloat dotX = (self.frame.size.width-totalWith)/2.f + (space+dotW)*i;
        CGFloat dotY = (self.frame.size.height-dotH)/2.f;
        dot.frame = CGRectMake(dotX, dotY, dotW, dotH);
    }
    
}

- (void)setCurrentPage:(NSInteger)currentPage {
    [super setCurrentPage:currentPage];
    [self updateDots];
}

@end

沒(méi)有提供任何的接口,用法和常規(guī)無(wú)異!
效果如圖:

Snip20160523_2.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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