bounds的深入研究

一.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]; }

 

转载于:https://www.cnblogs.com/naiwenmoer/p/5538235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值