user9009800
user9009800

Reputation:

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

Upvotes: 1

Views: 145

Answers (3)

Vinoth
Vinoth

Reputation: 9734

Get the ended state of UIGestureRecognizer and then show the view. See this doc

 @objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        if swipeGesture.state == .ended {
            viewFilter.isHidden = false
        }
    }
}

Upvotes: 0

Ratul Sharker
Ratul Sharker

Reputation: 8011

Use the UIGestureRecognizer.State appledoc

In your selector do something like this

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

        // code for looking up which state the gesture currently is in.
        switch swipeGesture.state {
            case .ended, .failed:
                viewFilter.isHidden = false
            // list up other cases here
        }

    }

}

Upvotes: 3

andesta.erfan
andesta.erfan

Reputation: 998

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

Upvotes: 0

Related Questions