API--UIScrollView API詳解

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIView.h>
#import <UIKit/UIGeometry.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIRefreshControl.h>
NS_ASSUME_NONNULL_BEGIN

UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateNormal NS_AVAILABLE_IOS(3_0);
UIKIT_EXTERN const CGFloat UIScrollViewDecelerationRateFast NS_AVAILABLE_IOS(3_0);

@class UIEvent, UIImageView, UIPanGestureRecognizer, UIPinchGestureRecognizer;
@protocol UIScrollViewDelegate;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding>

  //默認為 CGPointZero 設置滾動的偏移量
@property(nonatomic)         CGPoint                      contentOffset;

 // 默認為 CGSizeZero 設置滾動的區域
@property(nonatomic)         CGSize                       contentSize;

 // 內容視圖邊緣嵌入的間距
@property(nonatomic)         UIEdgeInsets                 contentInset;

    // 默認為nil 弱引用 UIScrollView的代理
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate>        delegate;

 //設置是否鎖定,這個屬性很有意思,默認為NO,當設置為YES時,你的滾動視圖只能同一時間在一個方向上滾動,但是當你從對角線拖動時,是時刻在水平和豎直方向同時滾動的。
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;

 // 默認為YES,回彈效果
@property(nonatomic)         BOOL                         bounces;

 // 默認為NO,豎直方向上的回彈效果
@property(nonatomic)         BOOL                         alwaysBounceVertical;

// 默認為NO,水平方向上的回彈效果
@property(nonatomic)         BOOL                         alwaysBounceHorizontal;

// 默認為NO. 是否開啟翻頁效果
@property(nonatomic,getter=isPagingEnabled) BOOL          pagingEnabled 

__TVOS_PROHIBITED;
 // 默認YES,是否可以拉動
@property(nonatomic,getter=isScrollEnabled) BOOL          scrollEnabled;

//默認為YES,展示水平或者豎直上的滑塊
@property(nonatomic)         BOOL                         showsHorizontalScrollIndicator;
@property(nonatomic)         BOOL                         showsVerticalScrollIndicator;

//設置滑動條的位置
@property(nonatomic)         UIEdgeInsets                 scrollIndicatorInsets;

//設置滑動條風格,枚舉如下:
@property(nonatomic)         UIScrollViewIndicatorStyle   indicatorStyle;
typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
    UIScrollViewIndicatorStyleDefault,     //默認
    UIScrollViewIndicatorStyleBlack,       //黑色風格
    UIScrollViewIndicatorStyleWhite        //白色風格
};

//  設置滑動速度  值域 0~1 值越小減速停止的時間越短
@property(nonatomic)         CGFloat                      decelerationRate NS_AVAILABLE_IOS(3_0);

//設置滾動視圖內容的偏移量,可以帶動畫效果
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
- 
// 設置滾動視圖滾動到某個可見區域,可以帶動畫效果
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;

//顯示一個短暫的滾動指示器
- (void)flashScrollIndicators;


//獲取用戶是否觸及視圖內容,如果已經觸摸,還沒移動返回yes
@property(nonatomic,readonly,getter=isTracking)     BOOL tracking;

//獲取用戶是否開始拖動視圖,如果開始滾動,到到達目標位置之間的時間內返回yes
@property(nonatomic,readonly,getter=isDragging)     BOOL dragging;

//獲取視圖是否開始減速(用戶停止拖動但視圖仍在滾動)
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating;

//設置視圖是否延遲處理觸摸事件(會將消息傳遞給子視圖),默認為yes,如果no,立刻會觸發-touchesShouldBegin:withEvent:inContentView:
//是個布爾值,當值是 YES 的時候,用戶觸碰開始,scroll view要延遲一會,看看是否用戶有意圖滾動。假如滾動了,那么捕捉 touch-down 事件,否則就不捕捉。
//假如值是NO,當用戶觸碰, scroll view 會立即觸發 touchesShouldBegin:withEvent:inContentView:,默認是 YES
@property(nonatomic) BOOL delaysContentTouches;

//當值是 YES 的時候,用戶觸碰后,然后在一定時間內沒有移動,scrollView 發送 tracking events,然后用戶移動手指足夠長度觸發滾動事件,
//這個時候,scrollView 發送了 touchesCancelled:withEvent: 到 subview,然后 scroView 開始滾動。假如值是 NO,scrollView 發送 tracking events 后,就算用戶移動手指,scrollView 也不會滾動
//設置是否給子視圖傳遞取消動作的消息(默認設置為YES,當scrollView觸發事件的時候,其子視圖不能觸發,如果設置為NO,則子視圖會繼續觸發事件)
@property(nonatomic) BOOL canCancelContentTouches;

//重寫這兩個方法可以控制起子視圖的事件響應
- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event inContentView:(UIView *)view;
- (BOOL)touchesShouldCancelInContentView:(UIView *)view;

//設置內容最小縮放比例,默認為1.0
@property(nonatomic) CGFloat minimumZoomScale;

//設置內容最大縮放比例,默認為1.0,必須大于最小縮放比例
@property(nonatomic) CGFloat maximumZoomScale;

//設置縮放比例,默認1.0
@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);

//設置是否可以縮放回彈,默認為YES
@property(nonatomic) BOOL  bouncesZoom;

//獲取是否正在縮放模式
@property(nonatomic,readonly,getter=isZooming)       BOOL zooming;

//獲取是否當前的縮放比例超出設置的峰值,
@property(nonatomic,readonly,getter=isZoomBouncing)  BOOL zoomBouncing;

//設置是否點擊狀態欄滾動到scrollView的最上端,默認yes
@property(nonatomic) BOOL  scrollsToTop __TVOS_PROHIBITED;

//只讀,移動的手勢
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);

//只讀,擠壓的手勢
@property(nullable, nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer NS_AVAILABLE_IOS(5_0);

// 只讀,定向媒體手勢
@property(nonatomic, readonly) UIGestureRecognizer *directionalPressGestureRecognizer UIKIT_AVAILABLE_TVOS_ONLY(9_0);

//設置鍵盤消失的模式,枚舉如下:
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode NS_AVAILABLE_IOS(7_0); 

// 默認為UIScrollViewKeyboardDismissModeNone
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    UIScrollViewKeyboardDismissModeNone,       // 不作為
    UIScrollViewKeyboardDismissModeOnDrag,      //手指滑動視圖鍵盤就會消失
    UIScrollViewKeyboardDismissModeInteractive, //手指滑動視圖后可以與鍵盤交互,上下滑動鍵盤會跟隨手指上下移動
};

//上拉下拉刷新視圖,可以自定義,下面有例子:
@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(10_0) __TVOS_PROHIBITED;

/*
- (void)viewDidLoad
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
    scrollView.userInteractionEnabled = TRUE;
    scrollView.scrollEnabled = TRUE;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(500, 1000);
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(Refresh:) forControlEvents:UIControlEventValueChanged];
    [scrollView addSubview:refreshControl];
    [self.view addSubview:scrollView];
}

- (void)Refresh:(UIRefreshControl *)refreshControl
{
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSThread sleepForTimeInterval:3];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"MMM d, h:mm a"];
            NSString *lastUpdate = [NSString stringWithFormat:@"Last updated on %@", [formatter stringFromDate:[NSDate date]]];
            refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdate];
            [refreshControl endRefreshing];
            NSLog(@"refresh end");
        });
    });
}
*/

@end

//協議部分
@protocol UIScrollViewDelegate<NSObject>
@optional
// 滑動時多次調用,offset值改變即滑動過程中,便會調用該代理函數
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- 
// 縮放時多次調用,zoomScale值改變即縮放過程中,便會調用該函數
- (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;

// 滾動動畫結束時調用,沒有動畫則不調用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;

// 返回將要縮放的UIView對象,調用多次
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

// 開始縮放時調用,只調用一次
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2);

// 已經結束縮放時調用,只調用一次
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale;

// 當用戶點擊狀態欄內容視圖是否滾動到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

// 滾動到頂部時調用
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;

@end
NS_ASSUME_NONNULL_END

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容