UIPanGesture识别器仅向一个方向滑动

时间:2018-09-27 04:46:33

标签: ios swift4 xcode9 uipangesturerecognizer

我正在使用UIPanGestureRecognizer,对我来说它正在工作。但是我这里有些问题,因为我是iOS的新手,只是从Android转到iOS。 首先看一下我想做什么:

我想要的内容:我有一个UITableView,并且要在单元格上执行滑动。我只想从左至右拖动它们并移动/删除该单元格。就像在android中一样。

但是我只想在一个方向上移动项目。那就是“从左到右”。但不是从右到左。现在在这里看看我到目前为止所做的事情

我所做的事情:

    @objc func handlePan(recognizer: UIPanGestureRecognizer) {
    // 1
    if recognizer.state == .began {
        // when the gesture begins, record the current center location
        originalCenter = center
        print("Center",originalCenter)

    }
    // 2
    if recognizer.state == .changed {
        let translation = recognizer.translation(in: self)


        center = CGPoint(x: originalCenter.x+translation.x, y: originalCenter.y)

        // has the user dragged the item far enough to initiate a delete/complete?
        deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
        completeOnDragRelease = frame.origin.x > frame.size.width / 2.0
        // print ("FrameX = ",frame.origin.x , " , ","Width = ",frame.size.width / 2.0 , "Total = ",frame.origin.x < -frame.size.width / 2.0 )
        //print ("DelOnDrag = ",deleteOnDragRelease , " , ","CompOnDrag = ",completeOnDragRelease)

    }
    // 3
    if recognizer.state == .ended {
        // the frame this cell had before user dragged it
        let originalFrame = CGRect(x: 0, y: frame.origin.y,
                                   width: bounds.size.width, height: bounds.size.height)
        if deleteOnDragRelease {
            if delegate != nil && clickedItem != nil {
                // notify the delegate that this item should be deleted
                delegate!.toDoItemDeleted(clickedItem: clickedItem!)
            }
        } else if completeOnDragRelease {

            UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
        } else {
            UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
        }
    }
}

我知道我可以对“ .changed”进行检查,并计算X值是趋向0还是小于0,然后趋向于0。但是点是一段时间后,它将使项目从右移到左。

问题:有什么方法可以获取接触点的x值?或者只是一些如何使用户想要从右向左滑动并阻止用户这样做的方式??请分享您的知识

2 个答案:

答案 0 :(得分:1)

您的相同代码只需将UIGestureRecognizer方法中的一项更改替换为该代码即可解决问题。只有从左到右的交换在tableview单元格上起作用。任何对此评分进行重新分类的查询都将在下面发表评论。

override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
            let translation = panGestureRecognizer.translation(in: superview!)
            if translation.x >=  0 {
                return true
            }
            return false
        }
        return false
    }

祝你好运。

保持编码。

答案 1 :(得分:0)

您可以使用以下扩展名

youradapterobject.notifyDataSetChanged();

您将获得如下所示的方向

extension UIPanGestureRecognizer {

    enum GestureDirection {
        case Up
        case Down
        case Left
        case Right
    }

    func verticalDirection(target: UIView) -> GestureDirection {
        return self.velocity(in: target).y > 0 ? .Down : .Up
    }

    func horizontalDirection(target: UIView) -> GestureDirection {
        return self.velocity(in: target).x > 0 ? .Right : .Left
    }   
}