UIScrollView继承UIView,它有两个子类,分别是UITableView和UITextView。UIScrollView有三个容易让人混淆的属性变量:
1、contentSize:scrollview可显示的区域
属性类型:
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
- @property(nonatomic) CGSize contentSize
- Description The size of the content view.
- Availability iOS (2.0 and later)
- Declared In UIScrollView.h
- Reference UIScrollView Class Reference
2、contentOffset:scrollview当前显示区域顶点相对于frame顶点的偏移量
属性类型:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
- @property(nonatomic) CGPoint contentOffset
- Description The point at which the origin of the content view is offset from the origin of the scroll view.
- Availability iOS (2.0 and later)
- Declared In UIScrollView.h
- Reference UIScrollView Class Reference
经过测试发现:CGFloat offsetY = scrollView.contentOffset.y;
当scrollview向下拉时,offsetY为负数;当上拉时,offsetY不断增大,当越过原点后会变成正数。
比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了4803、contentInset:scrollview的contentview的顶点相对于scrollview的位置
属性类型:
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
- @property(nonatomic) UIEdgeInsets contentInset
- Description The distance that the content view is inset from the enclosing scroll view.
- Availability iOS (2.0 and later)
- Declared In UIScrollView.h
- Reference UIScrollView Class Reference
它有点类似css中的padding。