iOS - UIScrollView 相关属性代理详解

时间:2022-01-04 05:05:36

一、UIScrollView的属性和代理方法详解

属性:

- (void)viewDidLoad {
[super viewDidLoad];
_scrollView.backgroundColor = [UIColor redColor];
//设置contentSize(必须设置)
_scrollView.contentSize = CGSizeMake(, );
//偏移量
_scrollView.contentOffset = CGPointMake(, );
//contentInset(在原有的基础上调整内容滚动区域的大小)
//_scrollView.contentInset = UIEdgeInsetsMake(-100, 0, 0, 0); //锁定方向(当系统识别滚动方向的时候,会把另外一个方向锁定,如果识别不了,就不锁定方向)
_scrollView.directionalLockEnabled = YES;
//反弹效果(默认为yes)
_scrollView.bounces = YES;
//默认是no,假如是yes并且bounces是yes,甚至如果内容大小小于bounds的时候,允许垂直拖动
_scrollView.alwaysBounceVertical = YES;
//分页(默认no)
_scrollView.pagingEnabled = YES;
//滚动(默认yes允许滚动)
_scrollView.scrollEnabled = YES; //滚动条
//showsHorizontalScrollIndicator默认yes 显示
_scrollView.showsHorizontalScrollIndicator = NO;
//滚动条滚动的范围(垂直滚动条滚动范围上下各缩小50)
_scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(, , , );
//样式
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
// Do any additional setup after loading the view, typically from a nib.
}

代理

- (void)viewDidLoad {
[super viewDidLoad];
//创建并添加scrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )];
[self.view addSubview:scrollView]; scrollView.backgroundColor = [UIColor redColor];
//设置内容视图的大小
scrollView.contentSize = CGSizeMake(, );
//设置内容视图
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
imageView.image = [UIImage imageNamed:@"new_feature_1"];
[scrollView addSubview:imageView]; _imageView = imageView;
//设置代理
scrollView.delegate = self; //[self performSelector:@selector(changeScrollViewContentOffSet:) withObject:scrollView afterDelay:3]; //设置缩放内容视图的缩放比例范围
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 1.5;
//当缩放比例超出范围之后,是否有反弹效果(no:不会超出范围,yes:超出范围之后反弹)
scrollView.bouncesZoom = YES; //属性设置滚动至顶部
//scrollView.scrollsToTop = YES;
// Do any additional setup after loading the view, typically from a nib.
} -(void)changeScrollViewContentOffSet:(UIScrollView *)scrollView{
[scrollView setContentOffset:CGPointMake(, ) animated:YES];
} #if 0
#pragma mark -UIScrollViewDelegate 滚动
//当scrollView滚动的时候,不停调用(可以监听scrollView的contentOffset)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
//NSLog(@"decelerating:%d",scrollView.decelerating);
//NSLog(@"dragging:%d",scrollView.dragging);
//NSLog(@"tracking:%d",scrollView.tracking);
NSLog(@"%s",__func__);
} //开始拖动的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//NSLog(@"tracking:%d",scrollView.tracking);
NSLog(@"%s",__func__);
} // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
//将要结束拖动
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
// NSLog(@"velocity:%@",NSStringFromCGPoint(velocity));
// NSLog(@"targetContentOffset:%@",NSStringFromCGPoint(*targetContentOffset));
//强制设置scrollView的偏移量
//*targetContentOffset = CGPointMake(0, 200);
NSLog(@"%s",__func__);
} // 已经结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//NSLog(@"dragging:%d",scrollView.dragging);
NSLog(@"%s",__func__);
} //将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//NSLog(@"decelerating:%d",scrollView.decelerating);
NSLog(@"%s",__func__);
} //减速完成(停止)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//NSLog(@"contentOffset:%@",NSStringFromCGPoint(scrollView.contentOffset));
NSLog(@"%s",__func__);
} // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
}
#endif
#pragma mark -UIScrollViewDelegate(缩放)
//指定缩放视图
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
NSLog(@"%s,zooming:%d",__func__,scrollView.zooming);
return _imageView;
}
//缩放之前调用
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view {
NSLog(@"%s,zooming:%d",__func__,scrollView.zooming);
}
//完成缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
NSLog(@"%s,zooming:%d",__func__,scrollView.zooming);
}
//缩放时调用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"%s,zooming:%d",__func__,scrollView.zooming);
} #pragma mark -UIScrollViewDelegate(滚动至顶部)
//允许滚动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
} //已经滚动至顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"加载新数据,刷新界面");
}

一、UIScrollView的重要的三大属性contentSize、contentInset、contentOffset

iOS - UIScrollView 相关属性代理详解

重要说明

(1)UIScrollView的frame与contentsize属性的区分:UIScrollView的frame指的是这个scrollview的可视范围(可看见的区域),contentsize是其滚动范围。

(2)contentinset(不带*号的一般不是结构体就是枚举),为UIScrollView增加额外的滚动区域。(上,左,下,右)逆时针。contentinset可以使用代码或者是视图控制器进行设置,但两者有区别(注意区分)。

(3)contentsize属性只能使用代码设置。

(4)contentoffset是个CGpoint类型的结构体,用来记录ScrollView的滚动位置,即记录着“框”跑到了哪里。知道了这个属性,就知道了其位置,可以通过设置这个属性来控制这个“框”的移动。