Scrollview

来源:互联网 发布:java高级家架构师培训 编辑:程序博客网 时间:2024/05/16 12:30
UIScrollView继承UIView,它有两个子类,分别是UITableView和UITextView。UIScrollView有三个容易让人混淆的属性变量:

1、contentSize: scrollview可显示的区域


属性类型:

struct CGSize {

    CGFloat width;

    CGFloat height;

};

typedef struct CGSize CGSize;

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @property(nonatomic) CGSize contentSize  
  2. Description The size of the content view.  
  3. Availability    iOS (2.0 and later)  
  4. Declared In UIScrollView.h  
  5. Reference   UIScrollView Class Reference  
假如一个scrollview的frame为(0,0,320,480),而它的contentSize为(320,960).也就是说,这个scrollview整个内容的大小为(320,960),要通过上下滑动scrollview来查看(320,480)后的内容。

2、contentOffset: scrollview当前显示区域顶点相对于frame顶点的偏移量

属性类型:

struct CGPoint {

    CGFloat x;

    CGFloat y;

};

typedef struct CGPoint CGPoint;

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @property(nonatomic) CGPoint contentOffset  
  2. Description The point at which the origin of the content view is offset from the origin of the scroll view.  
  3. Availability    iOS (2.0 and later)  
  4. Declared In UIScrollView.h  
  5. Reference   UIScrollView Class Reference  

经过测试发现:CGFloat offsetY = scrollView.contentOffset.y;

当scrollview向下拉时,offsetY为负数;当上拉时,offsetY不断增大,当越过原点后会变成正数。

比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480

3、contentInset: scrollview的contentview的顶点相对于scrollview的位置

属性类型:

typedef struct UIEdgeInsets {

    CGFloat top, left, bottom, right;

} UIEdgeInsets;

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. @property(nonatomic) UIEdgeInsets contentInset  
  2. Description The distance that the content view is inset from the enclosing scroll view.  
  3. Availability    iOS (2.0 and later)  
  4. Declared In UIScrollView.h  
  5. Reference   UIScrollView Class Reference  

它有点类似css中的padding。
0 0