UIScrollerView

一、枚举值

  1. UIScrollViewIndicatorStyle 滚动条风格
typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) {
    UIScrollViewIndicatorStyleDefault,     // 黑色滚动条白色边界
    UIScrollViewIndicatorStyleBlack,       // 黑色
    UIScrollViewIndicatorStyleWhite        // 白色
};
  1. UIScrollViewKeyboardDismissMode 键盘隐藏模式
typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {
    UIScrollViewKeyboardDismissModeNone, // 无
    UIScrollViewKeyboardDismissModeOnDrag,      // drag 时隐藏
    UIScrollViewKeyboardDismissModeInteractive, // 键盘跟随拖放触控屏幕,并可再次向上拉以取消取消
} NS_ENUM_AVAILABLE_IOS(7_0);
  1. UIScrollViewIndexDisplayMode
typedef NS_ENUM(NSInteger, UIScrollViewIndexDisplayMode) {
    UIScrollViewIndexDisplayModeAutomatic,    // 索引将根据需要自动显示或隐藏
    UIScrollViewIndexDisplayModeAlwaysHidden, // 索引将永远不会显示
} API_AVAILABLE(tvos(10.2));
  1. UIScrollViewContentInsetAdjustmentBehavior
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
    UIScrollViewContentInsetAdjustmentAutomatic, // 与.scrollableAxes类似,但为了向后兼容,当滚动视图由视图控制器拥有时,无论滚动视图是否可滚动,都会自动调整上下内容集
    UIScrollViewContentInsetAdjustmentScrollableAxes, // 调整可滚动轴的边缘(即, contentSize.width/height > frame.size.width/height 或 alwaysBounceHorizontal/Vertical = YES)
    UIScrollViewContentInsetAdjustmentNever, // 未调整contentInset
    UIScrollViewContentInsetAdjustmentAlways, // contentInset总是由滚动视图的safeareainset来调整
} API_AVAILABLE(ios(11.0),tvos(11.0));

二、属性

// scrollerView 的偏移量 默认为CGPointZero
@property(nonatomic) CGPoint contentOffset;

// scrollerView 的内容视图大小 默认为CGSizeZero
@property(nonatomic) CGSize contentSize;

// 在UIScrollView的四周增加额外的滚动区域 默认为UIEdgeInsetsZero
@property(nonatomic)  UIEdgeInsets contentInset;
1953382-c07ac3c52e80af42.png
@property(nonatomic, readonly) UIEdgeInsets adjustedContentInset API_AVAILABLE(ios(11.0),tvos(11.0));

@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));

@property(nonatomic,readonly,strong) UILayoutGuide *contentLayoutGuide API_AVAILABLE(ios(11.0),tvos(11.0));

@property(nonatomic,readonly,strong) UILayoutGuide *frameLayoutGuide API_AVAILABLE(ios(11.0),tvos(11.0));
// 滑动式图代理,默认为nil
@property(nullable,nonatomic,weak) id delegate; 
// 指定控件是否只能在一个方向上滚动                     
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;  
// 控制控件遇到边框是否反弹
@property(nonatomic)  BOOL  bounces;
// 默认为NO 控制垂直方向遇到边框是否反弹,如果为YES并且bounces是YES,即使内容小于界限,也允许垂直拖动
@property(nonatomic)  BOOL  alwaysBounceVertical; 
// 默认为NO 控制水平方向遇到边框是否反弹,如果为YES并且bounces是YES,即使内容小于界限,也允许水平拖动
@property(nonatomic) BOOL  alwaysBounceHorizontal;    
// 控制控件是否整页翻动
@property(nonatomic,getter=isPagingEnabled) BOOL  pagingEnabled __TVOS_PROHIBITED;
// 控制控件是否能滚动
@property(nonatomic,getter=isScrollEnabled) BOOL  scrollEnabled;
// 是否显示水平滚动条(滑动时显示滚动条滑动结束后消失),默认为YES
@property(nonatomic) BOOL  showsHorizontalScrollIndicator; 
// 是否显示垂直滚动条(滑动时显示滚动条滑动结束后消失),默认为YES
@property(nonatomic) BOOL   showsVerticalScrollIndicator;  
// 表示滚动指示器从封闭滚动视图中被嵌入的距离 ,默认为UIEdgeInsetsZero
@property(nonatomic)         UIEdgeInsets                 scrollIndicatorInsets;    
// 滚动条样式 默认为UIScrollViewIndicatorStyleDefault    
@property(nonatomic)         UIScrollViewIndicatorStyle   indicatorStyle;     
// 浮点数,规定用户提起手指后的滚动减速速率。           
@property(nonatomic)         UIScrollViewDecelerationRate decelerationRate NS_AVAILABLE_IOS(3_0);
// 索引展示类型  自动或者总是隐藏
@property(nonatomic)         UIScrollViewIndexDisplayMode indexDisplayMode API_AVAILABLE(tvos(10.2));

三、方法

// 短暂地显示滚动指示器。

- (void)flashScrollIndicators; 

// 滚动内容的指定区域以便使内容在接受器中可见。
// 参数: rect  定义内容视图区域的矩形。 animated 若滚动应被动画化则传入YES,否则为NO。
// 该方法滚动内容视图以使rect中定义的区域可以刚好显示在滚动视图中。若区域已经是可见的,该方法什么也不做。
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;  

// 设定内容视图原点相对于接收器原点的偏移。
//  参数 :  contentOffset 内容视图原点的偏移点(以点的形式表示)。animated 若YES,用一个恒定的速度以动画形式移动到新的偏移处;NO则立即移动。
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

// 浮点数,指定当前缩放因子。
// 参数: scale 要缩放内容到的新值。animated 若YES,动画化缩放到时新的缩放大小,NO则立即缩放。
//  新的缩放值应在minnumZoomScale和maximumZoomScale之间。
- (void)setZoomScale:(float)scale animated:(BOOL)animated

四、代理

  1. 滚动有关代理
// 滚动就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{    NSLog(@"只有scrollview是跟滚动状态就会调用此方法");
}
//开始拖拽时触发
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖拽");
    
}
// 结束拖拽时触发
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate{
        NSLog(@"结束拖拽");
}
// 开始减速时触发
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
        NSLog(@"开始减速");
    
}
// 结束减速时触发(停止)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        NSLog(@"结束减速(停止)");
}
  1. 缩放有关代理
//指定scrollview的某一个子视图为可缩放视图,前提条件是次视图已经添加到scrollview上面
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    UIView *imageView = (UIView*)[scrollView viewWithTag:1000];
    return imageView;
}

// 开始缩放的代理方法  第二个参数view:这个参数使我们将要缩放的视图(这里就是imageView)
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    NSLog(@"%@",view);
    
}

// 正在缩放的代理方法  只要在缩放就执行该方法,所以此方法会在缩放过程中多次调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    // 在缩放过程中为了使得该视图一直在屏幕中间,所以我们需要在他缩放的过程中一直调整他的center
    // 得到scrollview的子视图
    UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1000];
    // 打印imageView的frame,分析为什么他的位置会改变
  //  NSLog(@"frame -- %@",NSStringFromCGRect(imageView.frame));
    
    // 设置imageview的center,是他的位置一直在屏幕中央
    imageView.center = scrollView.center;
    // 打印contentSize  分析为什么缩放之后会滑动
    NSLog(@"contentSize %@",NSStringFromCGSize(scrollView.contentSize));
}


// 缩放结束所执行的代理方法
/**
 *  @ view    当前正在缩放的视图
 *  @ scale  当前正在缩放视图的缩放比例
 */
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    // 缩放完成之后恢复原大小,这里运用到2D仿射变换函数中与捏合有关的函数
    view.transform =CGAffineTransformMakeScale(1, 1);
       
    
}

你可能感兴趣的:(UIScrollerView)