UIScorllView

基本屬性

  • UIScrollView是一個(gè)可以滾動(dòng)的控件,功能強(qiáng)大.在顯示分頁(yè)圖片,商品列表,APP歡迎頁(yè)等地方會(huì)經(jīng)常用到.首先想使用uiscrollview必須先要?jiǎng)?chuàng)建一個(gè),并且設(shè)置frame.
// 創(chuàng)建UIScrollView
UIScrollView *scrollView = [UIScrollView alloc] init];
// 設(shè)置UIScrollView的frame
 self.scrollView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

以下是UIScrollView的常用屬性:

  • contenSize:用來(lái)設(shè)置scrollview的滾動(dòng)范圍
self.scrollView.contentSize = CGSizeMake(CGFloat width, CGFloat height);
  • contenOffset:用來(lái)設(shè)置scrollView內(nèi)控件的偏移量
self.scrollView.contentOffset =  CGPointMake(CGFloat x, CGFloat y);
  • contentInset:用來(lái)設(shè)置scrollView控件的外偏移量,防止scrollview的控件被其他控件擋住
self.scrollView.contentInset = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
  • showsHorizontalScrollIndicator:是否顯示水平方向滾動(dòng)條
// YES為顯示,NO為不顯示
self.scrollView.showsHorizontalScrollIndicator = YES/NO;
  • :showsVerticalScrollIndicator:是否顯示豎直方向滾動(dòng)條
// YES為顯示,NO為不顯示
self.scrollView.showsVerticalScrollIndicator = YES/NO;
  • 設(shè)置scrollView能否縮放
// 最小縮放比例
@property(nonatomic) CGFloat minimumZoomScale;  // default is 1.0
// 最大縮放比例
@property(nonatomic) CGFloat maximumZoomScale; 

代理模式

  • 如果想要監(jiān)聽(tīng)UIScrollView的滾動(dòng)事件,需要成為scrollView的代理.并且實(shí)現(xiàn)<UIScrollViewDelegate>中的方法即可.
// 可通過(guò)以下代碼設(shè)置代理,也可通過(guò)storyboard拖線的方式設(shè)置
self.scrolView.delegate = self;

注意:成為scrollView的代理后,需要遵守<UIScrollViewDelegate>協(xié)議.
以下是<UIScrollViewDelegate>協(xié)議中的常用的代理方法

 // any offset changes
// scrollView滾動(dòng)時(shí)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;  

// any zoom scale changes
// scrollView完成縮放時(shí)
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); 

// called on start of dragging (may require some time and or distance to move)
// 開(kāi)始拖拽時(shí)
- (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
// 即將停止拖拽時(shí)
- (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
// 停止拖拽時(shí)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

// called on finger up as we are moving
// 開(kāi)始減速時(shí)(停止拖拽,scrollview慣性減速時(shí))
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;  

// called when scroll view grinds to a halt
// 停止減速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      

// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//停止?jié)L動(dòng)動(dòng)畫(huà)
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; 

// return a view that will be scaled. if delegate returns nil, nothing happens
// 縮放scrollview里面的view時(shí)
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;  

// called before the scroll view begins zooming its content
// 開(kāi)始縮放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); 

// scale between minimum and maximum. called after any 'bounce' animations
// 停止縮放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; 

 // return a yes if you want to scroll to the top. if not defined, assumes YES
 //是否允許scrollview滾動(dòng)到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

// called when scrolling animation finished. may be called immediately if already at top
// 滾動(dòng)到頂部時(shí)
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      

注意:想監(jiān)聽(tīng)scrollView內(nèi)部的事件,必須成為scrollView的代理,并實(shí)現(xiàn)上述代理方法中對(duì)應(yīng)的方法.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容