你好,歡迎來到IOS教程網

 Ios教程網 >> IOS教程 >> 關於IOS教程 >> iOS App開發中的UIPageControl分頁控件使用小結

iOS App開發中的UIPageControl分頁控件使用小結

編輯:關於IOS教程

分頁控件是一種用來取代導航欄的可見指示器,方便手勢直接翻頁,最典型的應用便是iPhone的主屏幕,當圖標過多會自動增加頁面,在屏幕底部你會看到原點,用來只是當前頁面,並且會隨著翻頁自動更新。
一、創建
代碼如下:

UIPageControl* myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0.0, 400.0, 320.0, 0.0)]; 

二、設置屬性
頁面數目
代碼如下:

myPageControl.numberOfPages =5; 

 默認第一頁會被選中。如果要選擇其他頁,可以設置currentPage 屬性。頁面索引從 0 開始:
代碼如下:

myPageControl.currentPage =3;// 當前頁數,第四頁

默認情況下,即使只有一個頁面,指示器也會顯示進來。如果要在僅有一個頁面的情況下隱藏指示器,可以將 hideForSinglePage 的值設為 YES。
代碼如下:

myPageControl.hidesForSinglePage=YES; 

如果你希望直到有時間執行完你的操作之後,才更新當前指示器當前指示頁,可以將 defersCurrentPageDisPlay 設為YES。這樣的話你必須調用控件的 updateCurentPageDisPlay 來更新當前頁:
代碼如下:

myPageControl.defersCurrentPageDisplay = YES; 
    [myPageControl updateCurrentPageDisplay]; 

三、顯示控件
代碼如下:

[self.view addSubview:myPageControl]; 

四、通知
當用戶點觸分頁控件時,會產生一個 UIControlEventVakueChanged 事件。你可以用UIControl 類的 addTarget 方法,為其指定一個動作:
代碼如下:

-(void)pageChanged:(id)sender{ 
    UIPageControl* control = (UIPageControl*)sender; 
    NSInteger page = control.currentPage; 
    //添加你要處理的代碼 
}   
[myPageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; 

五、常用屬性一覽

代碼如下:

//創建UIPageControl
UIPageControl * page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50)];
 
//設置背景顏色
page.backgroundColor = [UIColor clearColor];
 
//設置小圓圈的個數
page.numberOfPages = 15;
 
//設置 小圓圈的顏色
page.pageIndicatorTintColor = [UIColor orangeColor];
 
//設置當前頁的小圓圈顏色
page.currentPageIndicatorTintColor = [UIColor redColor];
 
//獲取/更改當前頁
page.currentPage = 1;
 
//增加點擊事件
[page addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved