重寫UIPageControl實現自定義按鈕

2021-06-02 09:57:20 字數 1749 閱讀 7109

有時候uipagecontrol需要用到白色的背景, 那麼會導致上面的點按鈕看不見或不清楚,

我們可以通過繼承該類重寫函式來更換點按鈕的現實.

實現思路如下.

新建類繼承uipagecontrol :

@inte***ce mypagecontrol : uipagecontrol

- (id)initwithframe:(cgrect)frame;

@property (nonatomic, retain) uiimage *imagepagestatenormal;

@property (nonatomic, retain) uiimage *imagepagestatehighlighted;

@end

複製**

宣告了初始化該類的函式

接下來實現該類以及重寫父類方法:

@inte***ce mypagecontrol(private)  // 宣告乙個私有方法, 該方法不允許物件直接使用

- (void)updatedots;

@end

@implementation mypagecontrol  // 實現部分

@synthesize imagepagestatenormal;

@synthesize imagepagestatehighlighted;

- (id)initwithframe:(cgrect)frame

- (void)setimagepagestatenormal:(uiimage *)image

- (void)setimagepagestatehighlighted:(uiimage *)image

- (void)endtrackingwithtouch:(uitouch *)touch withevent:(uievent *)event

- (void)updatedots }}

- (void)dealloc

@end

複製**

ok, 在新增處加入以下來例項化該物件**:

mypagecontrol *pagecontrol = [[mypagecontrol alloc] initwithframe:cgrectmake(0,0, 200, 30)];

pagecontrol.backgroundcolor = [uicolor clearcolor];

pagecontrol.numberofpages = 5;

pagecontrol.currentpage = 0;

[pagecontrol setimagepagestatenormal:[uiimage imagenamed:@"pagecontrolstatenormal.png"]];

[pagecontrol setimagepagestatehighlighted:[uiimage imagenamed:@"pagecontrolstatehighlighted.png"]];

[self.view addsubview:pagecontrol];

[pagecontrol release];

複製**

搞定~發現個問題,如果直接做setcurrentpage的話,圖片就會失效了,所以覆寫setcurrentpage

// 覆寫setcurrentpage

- (void)setcurrentpage:(nsinteger)currentpage

複製**

這樣就ok了~

重寫UIPageControl實現自定義按鈕

新建類繼承uipagecontrol mypagecontrol.h檔案 inte ce mypagecontrol uipagecontrol id initwithframe cgrect frame property nonatomic,retain uiimage imagepagestat...

自定義UIPageControl 控制項 二

使用uipagecontrol的例子如前面所示。但光以控制項形式使用 uipagecontrol還是不好用,因為雖然使用者能通過手指掃動uipagecontrol控制項進行翻頁,但uipagecontrol控制項在螢幕上所佔的區域還是太小了,如果使用者在整個螢幕都能通過掃動來進行翻頁就更好了,這無疑...

自定義UIPageControl 控制項 一

1 新建專案usingpagecontrol。刪除mainwindow.xib檔案。在resources組中新增幾張,在這裡我隨便找了幾張動物的,你也可以另外找幾張。uiviewcontroller vc usingpagecontrolviewcontroller alloc init windo...