iOS UIScrollView的使用

來源:互聯網
上載者:User

標籤:

一、為什麼要用UIScrollView?

行動裝置的螢幕大小是極其有限的,因此直接展示在使用者眼前的內容也相當有限
當展示的內容較多,超出一個螢幕時,使用者可通過滾動手勢來查看螢幕以外的內容
普通的UIView不具備滾動功能,不適合顯示過多的內容。


UIScrollView是一個能夠滾動的視圖控制項,可以用來展示大量的內容,並且可以通過滾動查看所有的內容

系統設定就是一個ScrollView:

 

二、基本使用:

將需要展示的內容添加到UIScrollView中
設定UIScrollView的contentSize屬性,告訴UIScrollView所有內容的尺寸,也就是告訴它滾動的範圍(能滾多遠,滾到哪裡是盡頭)

       常用屬性:

@property(nonatomic)         CGPoint                      contentOffset;                  // default CGPointZero@property(nonatomic)         CGSize                       contentSize;                    // default CGSizeZero@property(nonatomic)         UIEdgeInsets                 contentInset;                   // default UIEdgeInsetsZero. add additional scroll area around content
contentOffset:這個屬性用來表示UIScrollView滾動的位置(其實就是內容左上方與scrollView左上方的間距值)
contentSize:這個屬性用來表示UIScrollView內容的尺寸,滾動範圍(能滾多遠)
contentInset:這個屬效能夠在UIScrollView的4周增加額外的捲動區域,一般用來避免scrollView的內容被其他控制項擋住

   其他屬性:

   @property(nonatomic) BOOL bounces;
  設定UIScrollView是否需要彈簧效果

   @property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
   設定UIScrollView是否能滾動

   @property(nonatomic) BOOL showsHorizontalScrollIndicator;
   是否顯示水平捲軸

   @property(nonatomic) BOOL showsVerticalScrollIndicator;
   是否顯示垂直捲軸

 

三、UIScrollView的代理

當UIScrollView發生一系列的滾動操作時, 會自動通知它的代理(delegate)對象,給它的代理髮送相應的訊息,讓代理得知它的滾動情況。所以想要監聽UIScrollView的滾動過程,就必須先給UIScrollView設定一個代理對象,然後通過代理得知UIScrollView的滾動過程。

UIScrollViewDelegate協議常用方法:
// 使用者開始拖拽時調用- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;// 滾動到某個位置時調用- (void)scrollViewDidScroll:(UIScrollView *)scrollView;// 使用者結束拖拽時調用- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

 

縮放的實現:

設定UIScrollView的id<UISCrollViewDelegate> delegate代理對象
設定minimumZoomScale :縮小的最小比例
設定maximumZoomScale :放大的最大比例
讓代理對象實現下面的方法,返回需要縮放的視圖控制項
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

 跟縮放相關的其他代理方法:

//縮放完畢的時候調用- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view//正在縮放的時候調用- (void)scrollViewDidZoom:(UIScrollView *)scrollView

 

 

分頁:
//只要將UIScrollView的pageEnabled屬性設定為YES,UIScrollView會被分割成多個獨立頁面,裡面的內容就能進行分頁展示//一般會配合UIPageControl增強分頁效果,UIPageControl常用屬性如下 一共有多少頁@property(nonatomic) NSInteger numberOfPages;//當前顯示的頁碼@property(nonatomic) NSInteger currentPage; //只有一頁時,是否需要隱藏頁碼指標@property(nonatomic) BOOL hidesForSinglePage; //其他頁碼指標的顏色@property(nonatomic,retain) UIColor *pageIndicatorTintColor;//當前頁碼指標的顏色@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor;

 

iOS UIScrollView的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.