iOS中系統(tǒng)默認(rèn)拖拽為1:1跟隨赌渣,即手指動的像素和scroll滑動的像素一致魏铅,但實(shí)際使用中我們可能需要調(diào)整拖拽跟隨速度,如:焦點(diǎn)式輪播坚芜、大小卡片輪播等一頁多個卡片的場景览芳,我們可能仍然希望手指橫向拖拽一屏僅滑動一個卡片的寬度。本文描述的正是此場景的解決方案鸿竖。
scrollView
本身的contentView
的滑動位置即contentOffset
是關(guān)聯(lián)到scrollView
的bounds
屬性上的沧竟,了解到這個點(diǎn)一切就順理解決了
以collectionView
為例,重寫其的setBounds
方法調(diào)整bounds
即可缚忧,代碼如下
@property (nonatomic, assign) CGPoint dragBeginPoint; // 拖拽開始點(diǎn)
@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];
}