当滑动手势停止视图问题

when swipe gesture stop view issue

我正在创建 swift 应用程序并且我正在使用 UISwipegesture 我正在向上和向下滑动方向并且它的工作完美但是当用户向上或向下滑动时我将隐藏显示视图并且它按预期隐藏和显示但是当滑动停止时,我希望该视图自动显示

让我展示我的代码以便更好地理解

代码

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

在这里你可以看到在上下方向上我隐藏了视图但是当滑动停止时我想要再次显示那个视图所以我无法理解如何做到这一点可以帮助我

您可以将 state 用于手势识别器:

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

使用 UIGestureRecognizer.State appledoc

在你的选择器中做这样的事情

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

    }

}

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