iOS UI

UIScrollView实现原理 以及和masonry配合使用

2018-06-20  本文已影响0人  沉默着欢喜丶

UIScrollView使用masonry布局, 必须得给一个固定的contentsize,如果像普通的uiview一样去布局,那么它将不会具备滚动的效果,只能当做view去使用。
因为UIScrollview内部实现原理是依托于已知contentsize去实现的。
使用masonry布局, 首先需要在scrollview上加一个子视图contentView,通过子视图的尺寸去撑起scrollview,这样scrollview就有了具体的contentsize。

具体实现代码:

myScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:myScrollView];
    myScrollView.backgroundColor = [UIColor whiteColor];
    [myScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        STRONGSELF
        make.edges.mas_equalTo(strongSelf.view);
    }];
    contentView = [[UIView alloc] init];
    [myScrollView addSubview:contentView];
    contentView.backgroundColor = [UIColor whiteColor];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(myScrollView);
        make.width.mas_equalTo(myScrollView);
        make.height.greaterThanOrEqualTo(@0.f);//此处保证容器View高度的动态变化 大于等于0.f的高度
    }];

然后将所有的子视图放在contentView上面,此处的例子是高度不固定,所以scrollview是上下滚动的,所以需要最下面的子视图来撑起contentview的底部。

UIScrollView实现原理

scrollView继承自UIView,检测手指滑动应该是在view上放置了一个手势识别。

- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] init];

[panGesture addTarget:self action:@selector(panGestureAction:)];

[self addGestureRecognizer:panGesture];

}

return self;

}

实现scrollview的滚动,首先要理解frame和bounds的概念。

提到它们,大家都知道frame是相对于父视图坐标系来说自己的位置和尺寸,bounds相对于自身坐标系来说的位置和尺寸,并且origin一般为(0,0)。

当我们尝试改变bounds的origin时,我们就会发现视图本身没有发生变化,但是它的子视图的位置却发生了变化,其实当我们改变bounds的origin的时候,视图本身位置没有变化,但是由于origin的值是基于自身的坐标系,所以自身坐标系的位置被我们改变了。而子视图的frame正是基于父视图的坐标系,当我们更改父视图bounds中origin的时候子视图的位置就发生了变化,这就是实现scrollView的关键点!!!

- (void)panGestureAction:(UIPanGestureRecognizer *)pan {

// 记录每次滑动开始时的初始位置

if (pan.state == UIGestureRecognizerStateBegan) {

self.startLocation = self.bounds.origin;

NSLog(@"%@", NSStringFromCGPoint(self.startLocation));

}

// 相对于初始触摸点的偏移量

if (pan.state == UIGestureRecognizerStateChanged) {

CGPoint point = [pan translationInView:self];

NSLog(@"%@", NSStringFromCGPoint(point));

CGFloat newOriginalX = self.startLocation.x - point.x;

CGFloat newOriginalY = self.startLocation.y - point.y;

CGRect bounds = self.bounds;

bounds.origin = CGPointMake(newOriginalX, newOriginalY);

self.bounds = bounds;

}

}

理解了上边内容的关键点,下边我们将对我们实现的scrollView做一个简单的优化。通过contentSize限制scrollView的内部空间,实现代码如下

if (newOriginalX < 0) {

newOriginalX = 0;

} else {

CGFloat maxMoveWidth = self.contentSize.width - self.bounds.size.width;

if (newOriginalX > maxMoveWidth) {

newOriginalX = maxMoveWidth;

}

}

if (newOriginalY < 0) {

newOriginalY = 0;

} else {

CGFloat maxMoveHeight = self.contentSize.height - self.bounds.size.height;

if (newOriginalY > maxMoveHeight) {

newOriginalY = maxMoveHeight;

}

通过contentOffset设置scrollView的初始偏移量,相信大家已经懂了如何设置偏移量了吧?没错我们只需设置view自身bounds的origin是实现代码如下:

- (void)setContentOffset:(CGPoint)contentOffset {

_contentOffset = contentOffset;

CGRect newBounds = self.bounds;

newBounds.origin = contentOffset;

self.bounds = newBounds;

}

https://www.jianshu.com/p/a9a1ca54ca54
关于scrollview原理的内容大部分转载了该作者的内容。

上一篇下一篇

猜你喜欢

热点阅读