ios – 滾動視圖(UIScrollView)詳解

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

移動的裝置的螢幕大小有限,當一個視圖要顯示的內容大於螢幕尺寸的時候就要用到滾動視圖,比如一個網頁的內容通常比螢幕尺寸大,那麼瀏覽器就使用了滾動視圖。

 

UIScrollView有一個contentSize屬性,聲明如下:

@property(nonatomic)         CGSize                       contentSize;

這個屬性工作表示滾動視圖的內容有多大。

建立滾動視圖執行個體:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.view.frame];    sv.contentSize = CGSizeMake(1000, 1000);    sv.backgroundColor = [UIColor redColor];    [self.view addSubview:sv];}

contentSize 屬性為{1000,1000},可以很明顯的看到橫向和縱向捲軸。

1. 滾動

視圖的滾動也沒什麼好說的,主要是設定一些屬性:


scrollEnabled  是否開啟滾動

scrollsToTop  預設為YES,使用者可以單擊狀態列滾動至頂部

bounces  預設為YES,當滾動至邊界時還可以進一步滾動,鬆開時迅速彈回至原位

directionalLockEnabled  如果為YES,僅可以同時在一個方向上滾動

decelerationRate  在輕彈螢幕後,滾動的速度逐漸衰減,較低的值衰減越快

showsHorizontalScrollIndicator、showsVerticalScrollIndicator  預設為YES,滾動時滾動指示條可見

indicatorStyle  捲軸的樣式(UIScrollViewIndicatorStyleBlack,UIScrollViewIndicatorStyleDefaultUIScrollViewIndicatorStyleWhite)

contentInset  指定內容四周的餘量(上 左 下 右的順序)

scrollIndicatorInsets  指定了滾動指示條在位置的切換

contentOffset  內容的錨點,當前顯示內容的左上方

2. 分頁

當內容大於螢幕時就可以分頁,設定pagingEnabled屬性為YES,滾動視圖就不允許使用者自由滑動,只能通過滑動至相鄰的部分。

3. 縮放

使滾動視圖可以縮放需要時設定minimumZoomScale和maximumZoomScale兩個屬性的值(預設為1),還需要在滾動視圖的委託中實現viewForZoomingInScrollView:方法,該方法返復原動視圖中哪個子視圖是可以縮放的,如果滾動視圖有多個子視圖,但是通常我們需要縮放整個滾動視圖的內容,那麼將多個視圖放在一個視圖即可,在將這個視圖成為滾動視圖的子視圖。

滾動視圖通過對捏合手勢的響應進行縮放,如果縮放超出了我們設定的限制,當手勢結束時,尺寸退回到我們設定的限制。如果想要嚴格的限制縮放不超過我們設定的限制,將bouncesZoom設定為NO就可以了。

在某種情況下,比如我們雙擊需要進行縮放的時候,就要用代碼進行縮放,使用下面兩個方法:

- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);

setZoomScale:animated:  根據比例縮放

zoomToRect:animated:  給定矩形的大小進行縮放

4. 委託

UIScrollViewDelegate可以接收大量訊息開協助你跟蹤滾動視圖的工作

下面是協議的定義

@protocol UIScrollViewDelegate<NSObject>@optional- (void)scrollViewDidScroll:(UIScrollView *)scrollView;                                               // any offset changes- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes// called on start of dragging (may require some time and or distance to move)- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;     // return a view that will be scaled. if delegate returns nil, nothing happens- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any ‘bounce‘ animations- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;   // return a yes if you want to scroll to the top. if not defined, assumes YES- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top@end

這些方法的意思就不解釋了,有注釋的,學習最快的方法就是看這些標頭檔。

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.