UIScrollView API详解

#import 
#import 
#import 
#import 
#import 
#import 
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 

  //默认为 CGPointZero 设置滚动的偏移量
@property(nonatomic)         CGPoint                      contentOffset;

 // 默认为 CGSizeZero 设置滚动的区域
@property(nonatomic)         CGSize                       contentSize;

 // 内容视图边缘嵌入的间距
@property(nonatomic)         UIEdgeInsets                 contentInset;

    // 默认为nil 弱引用 UIScrollView的代理
@property(nullable,nonatomic,weak) id        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 *)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
@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

你可能感兴趣的:(UIScrollView API详解)