UIScrollView

  • 移动设备屏幕有限,因此展示在用户眼前到内容也有限

  • 当展示内容较多,超出某个屏幕时,用户可通过滚动手势来查看屏幕外的内容

  • 普通UIView不具备滚动功能

  • UIScrollView:能滚动的视图控件,可以展示大量内容,并通过滚动查看

UIScrollView基本使用

ClipToBounds默认是YES,也就是自动隐藏超出边框的内容

  • 1. 将需要展示的内容添加到UIScrollView中

  • 2. 设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸(滚动范围,宽度和高度)

    • 如果想禁止某个方向的滚动,直接设置width或height为0

UIScrollView无法滚动的解决办法

  • 没有设置contentSize

  • scrolllEnabled = NO

  • 没有接收到触摸事件:userInteractionEnabled = NO

  • ......

UIScrollView常见属性

// 偏移量
@property (nonatomic)CGPoint cotentoffset;
用来表示UIScrollView滚动到位置
(其实就是内容左上角与scrollView左上角到间距值,包括x和y到值)
@property (nonatomic)CGSize contentSize;
用来表示UIScrollView内容到尺寸,滚动范围
@property(nonatomic)UIEdgeInsets contentInset;
这个属性能够在UIScrollVIew到四周增加额外到滚动区域额,一般用来避免scrollView到内容被其他控件挡住
缓慢偏移
 [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    在这里写入偏移代码
    [UIView commitAnimations];

缓慢偏移
    [UIView animateWithDuration:2.0 animations:^{
    在这里写入偏移代码
    }];
缓慢偏移并执行某方法
 [UIView animateWithDuration:2.0 animations:^{
    在这里写入偏移代码
    } completion:^(BOOL finished) {
    在这里写方法
    }];
用代理监听
[UIView setAnimationDelegate:self];
开始
    [UIView setAnimationWillStartSelector:<#(nullable SEL)#>];
结束
    [UIView setAnimationDidStopSelector:<#(nullable SEL)#>];

OC语法细节:不允许直接修改OC对象的结构体属性的成员

这里的y就是成员

self.scrollView.contentOffset.y = 0
这两个是不同的变量,存储空间不同
CGPoint offset = self.scrollView.contentOffset;
offset = offstrY ;
除非再赋值回去
self.scrollView.contentOffset = offset;

UIScrollView其他属性

@property(nonatomic) BOOL bounces;
设置UIScrollView是否需要弹簧效果

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
设置UIScrollView是否能滚动

@property(nonatomic) BOOL showsHorizontalScrollIndicator;
是否显示水平滚动条

@property(nonatomic) BOOL showsVerticalScrollIndicator;
是否显示垂直滚动条
用lastObject,first,或数组的索引取出UIScrollView中的某个子控件,结果可能不准确,因为UIScrollView中you两个滑块空间(水平和垂直滑块),并且在数组中的位置不固定,可以通过tag绑定或其他方式获取
CGRectGetMax(lastView.frame);

关闭UIScrollView滑块

 self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;

...

UIScrollView内部子控件添加约束的注意点:

  • 子控件的尺寸不能通过UIScrollView来计算,可以考虑通过以下方式计算
    • 可以设置固定值(width==100,height==300)
    • 可以相对于UIScrollView以外的其他控件来计算尺寸
  • UIScrollView的frame应该通过子控件以外的其他控件来计算
  • UIScrollView的contentSize通过子控件来计算
    • 根据子控件的尺寸以及子控件与UIScrollView之间的间距

results matching ""

    No results matching ""