您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

UIScrollView API 全解:滾動、反彈、縮放與 Delegate 時序

編輯:iOS 相關資訊

UIScrollView 是 iOS 滾動界面的基石,UITableView、UICollectionView、UITextView 全部繼承自它。把它的 API 摸熟,無數客製化滾動需求都能迎刃而解。

一、滾動指示器樣式

typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
    UIScrollViewIndicatorStyleDefault, // 黑底白邊,任何背景都清楚
    UIScrollViewIndicatorStyleBlack,   // 純黑,較小,適合淺色背景
    UIScrollViewIndicatorStyleWhite    // 純白,較小,適合深色背景
};

二、鍵盤收起方式(iOS 7 起)

typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    UIScrollViewKeyboardDismissModeNone,        // 預設,不自動收起
    UIScrollViewKeyboardDismissModeOnDrag,      // 開始拖動即收起鍵盤
    UIScrollViewKeyboardDismissModeInteractive  // 鍵盤跟手勢一起移出畫面,可反向拉回取消
};

@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0);

三、減速率常數

UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateNormal NS_AVAILABLE_IOS(3_0); // 預設
UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateFast   NS_AVAILABLE_IOS(3_0); // 甩動後更快停下

四、內容尺寸與偏移

@property(nonatomic) CGPoint     contentOffset;   // 內容原點的偏移
@property(nonatomic) CGSize      contentSize;     // 內容整體大小
@property(nonatomic) UIEdgeInsets contentInset;   // 內容四周內縮距離
@property(nonatomic, assign) id<UIScrollViewDelegate> delegate;

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; // 滾動到指定區域可見
- (void)flashScrollIndicators;  // 短暫閃現滾動條,常用於提示「還有更多內容」

五、邊界與反彈行為

@property(nonatomic, getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;
// 為 YES 時只能沿單一方向滾動,避免斜向漂移

@property(nonatomic) BOOL bounces;                 // 是否允許越過內容邊界後反彈
@property(nonatomic) BOOL alwaysBounceVertical;    // 內容雖小於視窗,仍允許垂直拖動
@property(nonatomic) BOOL alwaysBounceHorizontal;  // 同上,水平方向
@property(nonatomic) BOOL bouncesZoom;             // 縮放超出上下限時是否反彈

六、滾動與指示器開關

@property(nonatomic, getter=isPagingEnabled) BOOL pagingEnabled;      // 分頁滾動,一次一頁
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;      // 是否可滾動
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
@property(nonatomic) BOOL showsVerticalScrollIndicator;
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets;  // 指示器與邊緣的距離
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle;
@property(nonatomic) CGFloat decelerationRate NS_AVAILABLE_IOS(3_0);
@property(nonatomic) BOOL scrollsToTop;  // 點擊狀態列是否滾回頂部

七、狀態查詢(唯讀)

@property(nonatomic, readonly, getter=isTracking)     BOOL tracking;     // 手指正按在內容上
@property(nonatomic, readonly, getter=isDragging)     BOOL dragging;     // 正在拖動
@property(nonatomic, readonly, getter=isDecelerating) BOOL decelerating; // 正在減速滑行
@property(nonatomic, readonly, getter=isZooming)      BOOL zooming;      // 正在縮放
@property(nonatomic, readonly, getter=isZoomBouncing) BOOL zoomBouncing; // 縮放已超出範圍

八、觸碰判定

@property(nonatomic) BOOL delaysContentTouches;
// 延遲判定手勢,讓 scrollView 有機會先判斷是否為滾動手勢

@property(nonatomic) BOOL canCancelContentTouches;
// 為 YES 時,手指在子視圖上移動會讓 scrollView 送出 touchCancel 給子視圖
// 為 NO 時 scrollView 不介入,事件全交子視圖自行處理

// 供子類覆寫的切入點
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;

九、縮放

@property(nonatomic) CGFloat minimumZoomScale;  // 最小縮放比例
@property(nonatomic) CGFloat maximumZoomScale;  // 最大縮放比例
@property(nonatomic) CGFloat zoomScale NS_AVAILABLE_IOS(3_0);

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

縮放要真正生效,還必須在 delegate 中實作 viewForZoomingInScrollView: 回傳要縮放的視圖。

十、手勢識別器

@property(nonatomic, readonly) UIPanGestureRecognizer   *panGestureRecognizer
    NS_AVAILABLE_IOS(5_0);
@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer
    NS_AVAILABLE_IOS(5_0);

十一、UIScrollViewDelegate 完整時序

@protocol UIScrollViewDelegate <NSObject>
@optional
// 滾動中持續回呼,最高頻的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

// 縮放視圖變化時回呼
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2);

// 拖動階段
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
                targetContentOffset:(inout CGPoint *)targetContentOffset
    NS_AVAILABLE_IOS(5_0);
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

// 減速階段
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

// 以 setContentOffset:animated: 觸發的程式化動畫結束時回呼
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;

// 縮放相關
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
    NS_AVAILABLE_IOS(3_2);
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
                       withView:(UIView *)view atScale:(CGFloat)scale;

// 滾回頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
@end

幾個實務時序值得記住:

scrollViewWillEndDragging:withVelocity:targetContentOffset: 可以在手勢結束、減速尚未開始前,藉由 inout 參數直接改寫最終停止位置。客製化分頁效果(例如卡片吸附)最佳實作點就在這裡。

scrollViewDidEndDragging:willDecelerate: 的 decelerate 為 NO 時,代表不會進入減速階段,可以直接在此收尾;為 YES 時則要等 scrollViewDidEndDecelerating:。

scrollViewDidScroll: 是效能最敏感的方法,內部切勿做重計算、佈局或網路請求,必要時先做條件判斷或節流。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved