改变ColllectionView、TableView 拖拽跟随

2023-04-19  本文已影响0人  julieQY7

iOS中系统默认拖拽为1:1跟随,即手指动的像素和scroll滑动的像素一致,但实际使用中我们可能需要调整拖拽跟随速度,如:焦点式轮播、大小卡片轮播等一页多个卡片的场景,我们可能仍然希望手指横向拖拽一屏仅滑动一个卡片的宽度。本文描述的正是此场景的解决方案。

scrollView本身的contentView的滑动位置即contentOffset是关联到scrollViewbounds属性上的,了解到这个点一切就顺理解决了

collectionView为例,重写其的setBounds方法调整bounds即可,代码如下

@property (nonatomic, assign) CGPoint   dragBeginPoint; // 拖拽开始点
@property (nonatomic, assign) CGFloat   dragRatio;//期望的拖拽速率

- (void)setBounds:(CGRect)bounds
{
    if (self.isDragging && self.dragRatio > 0 && self.dragRatio < 1) {
        bounds.origin.x = (bounds.origin.x - self.dragBeginPoint.x) * self.dragRatio + self.dragBeginPoint.x;
    }
    [super setBounds:bounds];
}
上一篇 下一篇

猜你喜欢

热点阅读