![bounds的深入研究 bounds的深入研究](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
一.bounds的深入研究
1>frame:是以父控件的左上角为原点,描述的是一块区域的可视范围,
bounds:是以自己内容左上角为原点,描述的是可视范围在内容范围显示的区域
2>frame:参照父控件一直不变
bounds:参照内容,位置会变动
注意:当bounds的y值增加,内容会往上移动!为什么呢?因为y值增加,表示要显示下面的内容,所以内容往上移动了.
3>UIScrollView底层实现:
(1)先创建一个UIView,把它添加到控制器的view上
(2)再在这个UIView上添加一个拖拽手势
(3)拖动的时候调用一个方法,用来改变bounds的偏移量
#import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) UIView *scrollView
@property (nonatomic, assign) CGPoint offsetX;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UIView *scrollView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
_scrollView = scrollView;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[scrollView addGestureRecognizer:pan];
UISwitch *switchV = [[UISwitch alloc] init];
[scrollView addSubview:switchV];
} // 拖动的时候调用
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 获取偏移量
CGPoint transP = [pan translationInView:pan.view];
_offsetX.x += -transP.x;
_offsetX.y += -transP.y;
_scrollView.bounds = CGRectMake(_offsetX.x, _offsetX.y, self.view.bounds.size.width, self.view.bounds.size.height);
[pan setTranslation:CGPointZero inView:pan.view]; }