iOS開發UIScrollView使用詳解

iOS開發UIScrollView使用詳解

 

1、ScrollView經常使用方法和屬性

@property(nonatomic)CGPoint contentOffset;動畫

設置滾動的偏移量atom

 

@property(nonatomic)CGSize contentSize;spa

設置滑動區域.net

 

@property(nonatomic,assign) id<UIScrollViewDelegate>      delegate;3d

設置UIScrollView的代理代理

 

@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;code

設置是否鎖定,這個屬性頗有意思,默認爲NO,當設置爲YES時,你的滾動視圖只能同一時間在一個方向上滾動,可是當你從對角線拖動時,是時刻在水平和豎直方向同時滾動的。事件

 

@property(nonatomicBOOL bounces; ci

設置是否開啓回彈效果開發

 

@property(nonatomicBOOL alwaysBounceVertical;

是否開啓垂直方向的回彈效果

 

@property(nonatomic) BOOL alwaysBounceHorizontal;

是否開啓水平方向的回彈效果

 

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;

是否開啓翻頁效果

 

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;  

設置是否能夠滑動

 

@property(nonatomicBOOL showsHorizontalScrollIndicator;

設置是否顯示水平滑動條

 

@property(nonatomic) BOOL showsVerticalScrollIndicator;

設置是否顯示豎直滑動條

 

@property(nonatomic) UIEdgeInsets scrollIndicatorInsets;

設置滑動條的位置

 

@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle;

設置滑動條風格,枚舉以下:

typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
    UIScrollViewIndicatorStyleDefault,     //默認
    UIScrollViewIndicatorStyleBlack,       //黑色風格
    UIScrollViewIndicatorStyleWhite        //白色風格
};

 

@property(nonatomicCGFloat decelerationRate;

設置滑動速度

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;

設置滾動視圖內容的偏移量,能夠帶動畫效果

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;

設置滾動視圖滾動到某個可見區域,能夠帶動畫效果

 

- (void)flashScrollIndicators;

顯示一個短暫的滾動指示器

 

@property(nonatomic,readonly,getter=isTracking)     BOOL tracking;

獲取用戶是否觸及視圖內容

 

@property(nonatomic,readonly,getter=isDragging)     BOOL dragging;

獲取用戶是否開始拖動視圖

 

@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;

獲取視圖是否開始減速(用戶中止拖動但視圖仍在滾動)

 

@property(nonatomic) BOOL delaysContentTouches;

設置視圖是否延遲處理觸摸事件(會將消息傳遞給子視圖)

 

@property(nonatomic) BOOL canCancelContentTouches;

設置是否給子視圖傳遞取消動做的消息(默認設置爲YES,當scrollView觸發事件的時候,其子視圖不能觸發,若是設置爲NO,則子視圖會繼續觸發事件)

 

- (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;

設置縮放比例

 

- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated;

設置縮放比例,能夠帶動畫效果

 

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;

設置縮放顯示到某個區域,能夠帶動畫效果

 

@property(nonatomic) BOOL  bouncesZoom;

設置是否能夠縮放回彈

 

@property(nonatomic,readonly,getter=isZooming)       BOOL zooming; 

獲取是否正在縮放模式

 

@property(nonatomic,readonly,getter=isZoomBouncing)  BOOL zoomBouncing;

獲取是否當前的縮放比例超出設置的峯值

 

@property(nonatomic) BOOL  scrollsToTop;

設置是否點擊狀態欄滾動到scrollView的最上端

 

@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode;

設置鍵盤消失的模式,枚舉以下:

typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    UIScrollViewKeyboardDismissModeNone,
    UIScrollViewKeyboardDismissModeOnDrag,      //手指滑動視圖鍵盤就會消失
    UIScrollViewKeyboardDismissModeInteractive, //手指滑動視圖後能夠與鍵盤交互,上下滑動鍵盤會跟隨手指上下移動
};

 

 

 

2、ScrollViewDelegata中經常使用方法

 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView; 

 

視圖已經開始滑動時觸發的方法

 

- (void)scrollViewDidZoom:(UIScrollView *)scrollView;

視圖已經開始縮放時觸發的方法

 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

視圖開始拖動時觸發的方法

 

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset;

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

視圖拖動結束時觸發的方法

 

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; 

視圖開始減速時觸發的方法

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; 

視圖減速結束時觸發的方法

 

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;

視圖動畫結束時觸發的方法,使用set方法設置偏移量後回觸發

 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

返回進行縮放的視圖

 

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view;

視圖內容將要開始縮放時觸發的方法

 

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale;

視圖內容結束縮放時觸發的方法

 

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; 

返回yes,開啓快捷滾動回頂端,將要滾動時調用

 

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;

視圖快捷滾動回頂端開始動做時調用

 

疏漏之處 歡迎指正

 

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索