3

I have a page that retrieves data from the database to display info about the current info or previous or after

It goes that

if Status = 1 it shows the current
Status = 2 it shows previous
Status = 0 it shows next

What I did so far is display the current data and it shows perfectly I need to do a feature that when the user swipes right to left shows next and left to right shows previous

Any Idea or guideline just from where to start

Thank you

MuaathAli
  • 117
  • 4
  • 15

2 Answers2

12
override func viewDidLoad()
{
super.viewDidLoad()

let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.view.addGestureRecognizer(swipeLeft)

}


func respondToSwipeGesture(gesture: UIGestureRecognizer)
{
    if let swipeGesture = gesture as? UISwipeGestureRecognizer
    {
        switch swipeGesture.direction
        {
            case UISwipeGestureRecognizerDirection.Right:
                 //write your logic for right swipe
                  print("Swiped right")

            case UISwipeGestureRecognizerDirection.Left:
                 //write your logic for left swipe
                  print("Swiped left")

            default:
                break
        }
    }
}
sumeet
  • 157
  • 1
  • 5
  • 2
    In the example code above you could just use one gesture recognizer and set the direction as follows `swipeRight.direction = [UISwipeGestureRecognizerDirection.left, UISwipeGestureRecognizerDirection.right]` – leafcutter Sep 22 '17 at 15:46
  • I recieve signal SIGABRT :( – Ahmadreza Nov 01 '17 at 18:10
-1
class WelcomeController: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet var backgroundView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create left swipe gesture recognizer
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
        leftSwipe.direction = .left
        backgroundView.addGestureRecognizer(leftSwipe)
        
        // Create right swipe gesture recognizer
        let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(_:)))
        rightSwipe.direction = .right
        backgroundView.addGestureRecognizer(rightSwipe)
        
    }
    
    @objc func handleSwipe(_ gestureRecognizer: UISwipeGestureRecognizer) {
        if gestureRecognizer.direction == .left {
            // Handle left swipe
        } else if gestureRecognizer.direction == .right {
            // Handle right swipe
        }
    }
}

// Use ChatGPT
Chinedu Ofor
  • 707
  • 9
  • 11