iosswiftuiswipegesturerecognizer

when swipe gesture stop view issue


I am creating swift application and i am using UISwipegesture i am swiping up and down direction and its work perfectly but when usewr swipe up or down i will hide show view and it hide and show as expected but when swiping stopped i want that view show automatically

let me Show my code for better understanding

Code

videDidLoad()
    let swipe = UISwipeGestureRecognizer(target: self, action: 
    #selector(respondToSwipeGesture(gesture:)))
    swipe.direction = UISwipeGestureRecognizer.Direction.up
    swipe.delegate = self
    self.view.addGestureRecognizer(swipe)

    let swipe1 = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
    swipe1.direction = UISwipeGestureRecognizer.Direction.down
    swipe1.delegate = self
    self.view.addGestureRecognizer(swipe1)



  @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.up:
                print("Swiped up")
               viewFilter.isHidden = true
            case UISwipeGestureRecognizer.Direction.down:
                print("Swiped down")
               viewFilter.isHidden = true
            default:
                break
            }
        }
    }

here you can able to see that on up down direction i hide view but when swipe stop i want that again show that view so i cant under stand how to do that can any please help me


Solution

  • You can use state for gesture recognizer:

        @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizer.Direction.up:
                print("Swiped up")
                viewFilter.isHidden = true
            case UISwipeGestureRecognizer.Direction.down:
                print("Swiped down")
                viewFilter.isHidden = true
            default:
                break
            }
            if swipeGesture.state == .ended {
                viewFilter.isHidden = false
            }
        }
    }