scrollView常用屬性

  • contentSize: 內容大小,設置了纔可以滾動
  • contentOffset: 內容偏移位置
  • contentInset: 內容間距
  • pagingEnabled: 是否允許分頁
  • bounces: 是否允許彈簧效果
  • showsHorizontalScrollIndicator: 是否允許顯示水平指示器(滾動條)
  • showsVerticalScrollIndicator: 是否允許顯示垂直指示器(滾動條)

  • 滾動內容屬性
/// 內容偏移位置
@property(nonatomic)         CGPoint                      contentOffset;                  // default CGPointZero
/// 內容大小,設置了纔可以滾動
@property(nonatomic)         CGSize                       contentSize;                    // default CGSizeZero
/// 內容間距
@property(nonatomic)         UIEdgeInsets                 contentInset;
  • 委託屬性
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate>        delegate;
要想監聽滾動視圖的滾動 / 拖拽 / 縮放 / 狀態欄交互

需要設置 delegate
實現相關協議方法
  • 彈簧效果屬性
/// 允許彈簧效果
@property(nonatomic)         BOOL                         bounces;                        // default YES. if YES, bounces past edge of content and back again
/// 始終允許垂直彈
@property(nonatomic)         BOOL                         alwaysBounceVertical;           // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
/// 始終允許水平彈
@property(nonatomic)         BOOL                         alwaysBounceHorizontal;
  • 分頁屬性
/// 允許分頁
@property(nonatomic,getter=isPagingEnabled) BOOL          pagingEnabled __TVOS_PROHIBITED;// default NO. if YES, stop on multiples of view bounds
  • 指示器屬性
/// 顯示水平指示器
@property(nonatomic)         BOOL                         showsHorizontalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
/// 顯示垂直指示器
@property(nonatomic)         BOOL                         showsVerticalScrollIndicator;   // default YES. show indicator while we are tracking. fades out after tracking
/// 指示器間距
@property(nonatomic)         UIEdgeInsets                 scrollIndicatorInsets;          // default is UIEdgeInsetsZero. adjust indicators inside of insets
/// 指示器樣式
@property(nonatomic)         UIScrollViewIndicatorStyle   indicatorStyle;                 // default is UIScrollViewIndicatorStyleDefault
在 scrollView 中指示器本質上就是 UIImageView

通過查看視圖層次結構可以看到
  • 縮放屬性
/// 最小縮放比例
@property(nonatomic) CGFloat minimumZoomScale;     // default is 1.0
/// 最大縮放比例
@property(nonatomic) CGFloat maximumZoomScale;     // default is 1.0. must be > minimum zoom scale to enable zooming
要允許縮放

必須設置以上兩個屬性
同時遵守協議
實現協議方法 -viewForZoomingInScrollView:
  • 狀態欄屬性
/// 點擊狀態欄滾動到頂部
@property(nonatomic) BOOL  scrollsToTop __TVOS_PROHIBITED;          // default is YES.
一個視圖中,如果有多個 scrollView

只有唯一一個 scrollView 的 scrollsToTop 屬性設置爲 YES,才支持點擊狀態欄滾動到頂部
  • 鍵盤屬性
/// 鍵盤解除模式
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); // default is UIScrollViewKeyboardDismissModeNone
如果是 UITextView,通常需要將 alwaysBounceVertical 屬性設置爲 YES

typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    // 無
    UIScrollViewKeyboardDismissModeNone,
    // 拖拽關閉鍵盤
    UIScrollViewKeyboardDismissModeOnDrag,      // dismisses the keyboard when a drag begins
    // 必須要拖拽到鍵盤纔可以關閉鍵盤,很少使用
    UIScrollViewKeyboardDismissModeInteractive, // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
} NS_ENUM_AVAILABLE_IOS(7_0);
  • 常用方法
/// 動畫設置偏移位置
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;  // animate at constant velocity to new offset

/// 動畫設置滾動區域
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;         // scroll so rect is just visible (nearest edges). nothing if rect completely visible
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章