創(chuàng)建
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 50, 200, 800)];
基礎(chǔ)屬性
// 背景色
scrollView.backgroundColor = [UIColor redColor];
// 內(nèi)容視圖的大小
// 如果不設(shè)置該屬性,scrollView無(wú)法滑動(dòng)(即使添加了超出控件大小的子視圖依然不能滑動(dòng))
scrollView.contentSize = CGSizeMake(200, 400);
// 內(nèi)容視圖的偏移量
scrollView.contentOffset = CGPointMake(30, 30); // (30,30)偏移量代表控件左上角位于內(nèi)容視圖,左30下30,負(fù)值方向相反
// 如果想在設(shè)置內(nèi)容視圖偏移量的時(shí)候有動(dòng)畫(huà)就用這個(gè)方法
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;

示意圖:紅色為內(nèi)容視圖,黑框?yàn)榭丶笮?此時(shí)偏移量為(30,30)
// 滾動(dòng)到矩形可見(jiàn)的最近邊緣
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
// 內(nèi)容視圖邊緣嵌入的間距
scrollView.contentInset = UIEdgeInsetsMake(30, 30, 10, 10); // UIEdgeInsetsMake(上, 左, 下, 右)

示意圖:紅色為內(nèi)容視圖,黃色為嵌入間距區(qū)域,黑框?yàn)榭丶?藍(lán)色為添加在scrollView上的子視圖(注意子視圖的frame)
// 彈簧效果
scrollView.bounces = NO; // 默認(rèn)為YES 取消設(shè)置NO
// 當(dāng)bounces屬性為NO時(shí),以下兩個(gè)屬性設(shè)置無(wú)效
scrollView.alwaysBounceHorizontal = YES; // 水平方向彈簧效果
scrollView.alwaysBounceVertical = YES; // 垂直方向彈簧效果
// 減速的速度
scrollView.decelerationRate = 0.1; // 值域 0~1 值越小減速停止的時(shí)間越短
// 只在一個(gè)方向上滾動(dòng)
scrollView.directionalLockEnabled = YES; // 默認(rèn)為NO,設(shè)置為YES內(nèi)容視圖只在一個(gè)方向上滾動(dòng)
// 是否能滾動(dòng)
scrollView.scrollView = NO; // 默認(rèn)為YES,設(shè)置NO不能滑動(dòng)
// 點(diǎn)擊狀態(tài)欄滾動(dòng)到頂部
scrollView.scrollsToTop = YES; // 默認(rèn)為YES,設(shè)置為NO點(diǎn)擊轉(zhuǎn)態(tài)欄不能滑動(dòng)到頂部
// 整頁(yè)滾動(dòng)
scrollView.pagingEnabled = YES; // 默認(rèn)為NO,設(shè)置YES內(nèi)容視圖整頁(yè)滾動(dòng)
// 延遲內(nèi)容觸動(dòng)
scrollView.delaysContentTouches = YES;
// 是個(gè)布爾值,當(dāng)值是 YES 的時(shí)候,用戶(hù)觸碰開(kāi)始,scroll view要延遲一會(huì),看看是否用戶(hù)有意圖滾動(dòng)。假如滾動(dòng)了,那么捕捉 touch-down 事件,否則就不捕捉。假如值是NO,當(dāng)用戶(hù)觸碰, scroll view 會(huì)立即觸發(fā) touchesShouldBegin:withEvent:inContentView:,默認(rèn)是 YES
參考:http://blog.csdn.net/ch_soft/article/details/6947695
// 取消內(nèi)容觸動(dòng)
scrollView.canCancelContentTouches = YES;
// 當(dāng)值是 YES 的時(shí)候,用戶(hù)觸碰后,然后在一定時(shí)間內(nèi)沒(méi)有移動(dòng),scrollView 發(fā)送 tracking events,然后用戶(hù)移動(dòng)手指足夠長(zhǎng)度觸發(fā)滾動(dòng)事件,這個(gè)時(shí)候,scrollView 發(fā)送了 touchesCancelled:withEvent: 到 subview,然后 scroView 開(kāi)始滾動(dòng)。假如值是 NO,scrollView 發(fā)送 tracking events 后,就算用戶(hù)移動(dòng)手指,scrollView 也不會(huì)滾動(dòng)
// 隱藏鍵盤(pán)樣式
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
UIScrollViewKeyboardDismissModeNone, // 不作為
UIScrollViewKeyboardDismissModeOnDrag, // 拖拽視圖隱藏鍵盤(pán)
UIScrollViewKeyboardDismissModeInteractive, // 交互式的隱藏
} NS_ENUM_AVAILABLE_IOS(7_0);
// 獲取各種狀態(tài)
@property(nonatomic,readonly,getter=isTracking) BOOL tracking; // 當(dāng)touch后還沒(méi)有拖動(dòng)的時(shí)候值是YES,否則NO
@property(nonatomic,readonly,getter=isDragging) BOOL dragging; // 是否正在拖動(dòng)
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating; // 是否正在減速
@property(nonatomic,readonly,getter=isZooming) BOOL zooming; // 是否正在縮放
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing; // 當(dāng)內(nèi)容放大到最大或者最小產(chǎn)生反彈效果時(shí)候值是YES,否則NO
// 相關(guān)手勢(shì)
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);
// 拖拽手勢(shì)
@property(nullable, nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);
// 捏合手勢(shì)
@property(nonatomic, readonly) UIGestureRecognizer *directionalPressGestureRecognizer UIKIT_AVAILABLE_TVOS_ONLY(9_0);
// 定向媒體手勢(shì)
滾動(dòng)指示條相關(guān)
// 滾動(dòng)條樣式
scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
UIScrollViewIndicatorStyleDefault, // 默認(rèn)樣式
UIScrollViewIndicatorStyleBlack, // 黑色樣式
UIScrollViewIndicatorStyleWhite // 白色樣式
};
// 隱藏、顯示滾動(dòng)指示條
scrollView.showsHorizontalScrollIndicator = NO; // 水平方向
scrollView.showsVerticalScrollIndicator = NO; // 垂直方向
// 默認(rèn)值都是YES,隱藏設(shè)置為NO
// 調(diào)用滾動(dòng)指示條
- (void)flashScrollIndicators; // 調(diào)用這個(gè)方法 滾動(dòng)條會(huì)顯示一下
// 滾動(dòng)條位置
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(30, 30, 0, 0);
// UIEdgeInsetsMake(top, left, bottom, right) top、bottom分別是垂直方向滾動(dòng)條距上端下端距離,left、right分別是水平方向滾動(dòng)條距左端右端距離
縮放相關(guān)
// 縮放比例
scrollView.zoomScale = 1.5f; // 縮放比例
scrollView.minimumZoomScale = 0.5f; // 最小縮放比例
scrollView.maximumZoomScale = 3.0f; // 最大縮放比例
// 縮放反彈
scrollView.bouncesZoom = NO; // 默認(rèn)為YES,設(shè)置為NO取消縮放到極限時(shí)產(chǎn)生反彈效果
代理函數(shù)
// 設(shè)置代理
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate;
// 代理函數(shù)
// 滑動(dòng)時(shí)多次調(diào)用,offset值改變即滑動(dòng)過(guò)程中,便會(huì)調(diào)用該代理函數(shù)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
// 縮放時(shí)多次調(diào)用,zoomScale值改變即縮放過(guò)程中,便會(huì)調(diào)用該函數(shù)
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2);
// 開(kāi)始滑動(dòng)時(shí)調(diào)用,只調(diào)用一次,手指不松開(kāi)只算一次
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
// 結(jié)束滑動(dòng)時(shí)調(diào)用,只調(diào)用一次,手指離開(kāi)時(shí)執(zhí)行
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
// 已經(jīng)結(jié)束滾動(dòng)時(shí)調(diào)用,只調(diào)用一次,當(dāng)手指離開(kāi)達(dá)成時(shí)執(zhí)行
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
// 開(kāi)始減速時(shí)調(diào)用,只調(diào)用一次,當(dāng)內(nèi)容視圖開(kāi)始減速狀態(tài)執(zhí)行
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
// 結(jié)束減速時(shí)調(diào)用,只調(diào)用一次,當(dāng)內(nèi)容視圖結(jié)束減速狀態(tài)執(zhí)行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
// 滾動(dòng)動(dòng)畫(huà)結(jié)束時(shí)調(diào)用,沒(méi)有動(dòng)畫(huà)則不調(diào)用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
// 返回將要縮放的UIView對(duì)象,調(diào)用多次
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
// 開(kāi)始縮放時(shí)調(diào)用,只調(diào)用一次
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2);
// 已經(jīng)結(jié)束縮放時(shí)調(diào)用,只調(diào)用一次
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale;
// 當(dāng)用戶(hù)點(diǎn)擊狀態(tài)欄內(nèi)容視圖是否滾動(dòng)到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
// 滾動(dòng)到頂部時(shí)調(diào)用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 ,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!