IOS 之 UIScrollView,UIPageControl

來源:互聯網
上載者:User

標籤:

UIScrollView 可以用於顯示多於一個螢幕的內容,超出螢幕範圍的內容可以通過滑動進行查看,UIPageControl 類提供一行點來指示當前顯示的是多整頁模式的哪一頁。

下面實現一個簡單的圖片自動播放器為例說說 UIScrollView 與 UIPageControl類中的一些方法。

注意:下述代碼中的 scrollY,kScreenWidth,kScreenHeight,kImgCount 等都是定義好的常量

#define kScreenHeight [UIScreen mainScreen].bounds.size.height#define kScreenWidth [UIScreen mainScreen].bounds.size.widthCGFloat kImgCount = 10;CGFloat scrollY = 10;CGFloat pageCtrlWidth = 200;

frame中的size指UIScrollView的可視範圍

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, scrollY, kScreenWidth, kScreenHeight - scrollY)];

設定UIScrollView的滾動範圍(大小)

self.scrollView.contentSize = CGSizeMake(kScreenWidth * kImgCount, kScreenHeight - scrollY);

控制控制項整頁翻動(分頁效果)

self.scrollView.pagingEnabled = YES;

UIScrollView當前滾動的位置,animated:YES 表示以恒定速度滾動

[self.scrollView setContentOffset:point animated:YES];

例如:拖動到一半時,頁面數加1。

NSInteger page = scrollView.contentOffset.x / kScreenWidth + 0.5;//contentOffset.x 表示在x軸上拖動的距離    self.pageCtrl.currentPage = page;

初始化UIPageControl

self.pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - pageCtrlWidth)/2, kScreenHeight - scrollY, kScreenWidth/2, scrollY/2 )];

一共顯示多少個圓點

self.pageCtrl.numberOfPages = kImgCount;

設定非選中頁的圓點顏色

self.pageCtrl.pageIndicatorTintColor = [UIColor greenColor];

設定選中頁的圓點顏色

self.pageCtrl.currentPageIndicatorTintColor = [UIColor yellowColor];

A insertSubView B aboveSubview:C 是將B插入A並且在A已有的子視圖C的上面
A addSubview B 是將B直接覆蓋在A的最上層
A insertSubView B AtIndex:2 是將B插入到A的子視圖index為2的位置(最底下是0)
A insertSubView B belowSubview:C 是將B插入A並且在A已有的子視圖C的下面

例:

[self.view insertSubview:self.pageCtrl aboveSubview:self.scrollView];

暫時更新 UIScrollView 與 UIPageControl 的這些方法,當然還有一些其他方法沒有整理,後續會根據學習情況不斷積累!

IOS 之 UIScrollView,UIPageControl

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.