在垂直UICollectionView滚动上禁用水平UICollectionViewCell平移手势

时间:2018-04-06 21:14:04

标签: ios swift uicollectionview uigesturerecognizer uicollectionviewcell

我有一个可垂直滚动的UICollectionView,其中的单元格具有添加的水平平移手势。

是否有建议的方法在滚动UICollectionView时禁用水平平移手势?我问,因为每当我垂直滚动时,最左边或右边的最轻微的动作也会触发平移手势并向右或向左移动单元格。

我尝试在手势状态发生变化时启用/禁用UICollectionView,但由于几乎每个触摸实例都会读取平移手势,因此难以滚动。我也尝试过在线评估x / y速度并确定是否完成平移手势。这似乎阻止了任何水平平移..

最后,我需要使用平移与滑动,因为当有人平移时(即没有走x距离,将单元格返回到原始位置),我需要在平移阈值上包含条件语句。如果有滑动的方式,我全都是耳朵!

以下相关代码:

func onPan(_ sender: UIPanGestureRecognizer) {
    //Attempt 1
    theCollectionView.isScrollEnabled = false

    let cellView = sender.view!
    let point = sender.translation(in: cellView)
    cellView.center = CGPoint(x: parentView.center.x + point.x, y: cellView.center.y)

    if sender.state == .ended {
        //Attempt 1
        homeCollectionView.isScrollEnabled = true

        UIView.animate(withDuration: 0.2, animations: {
            cellView.center = CGPoint(x: self.parentView.center.x, y: cellView.center.y)
        })
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

//Attempt 2
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return abs((pan.velocity(in: pan.view)).x) > abs((pan.velocity(in: pan.view)).y)
}

0 个答案:

没有答案
相关问题