Search code examples
iosswift4xcode9uipangesturerecognizer

UIPanGesture recognizer swiping only in one direction


I am working on UIPanGestureRecognizer and to me it is working. but I have some problem here as I am new to iOS and just shifted from Android to iOS. First take a look at what I want to do:

What I want: I have a UITableView and I want to perform swiping on the Cells. I just want to drag them from left to right side and move/Delete that cell. Pretty same like it is done in android.

But I just want to move the item only in one direction. And that is "LEFT TO RIGHT". But not from right to left. Now here take a look at what I have done so far

What I have Done:

    @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})
        }
    }
}

I know I can make a check on ".changed" , and calculate if the X value is going towards 0 or lesser then 0. But point is for some time it will move item from right to left.

Question: Is there any way I can get the x value of point of contact? or just some how I can get user want to swipe right to left and just stop user from doing that?? Please share your knowledge


Solution

  • your same code just one changes in your UIGestureRecognizer method replace with this code and your problem solve. only left to right side swap work on your tableview cell . any query regrading this just drop comment below.

    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
        }
    

    Good Luck.

    Keep coding.