在当前视图控制器内的子视图上未调用滑动手势

Swipe gesture is not being called on a subview inside a present view controller

我展示了一个视图控制器,里面有一个子视图。我在子视图上添加了一个滑动手势。未调用滑动手势。相反,呈现的视图控制器试图关闭自身,即向下。如何覆盖子视图而不是超级视图识别的滑动​​手势。 当我在静态视图控制器上实现滑动时,它按预期工作。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var viewSwipe: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp(_:)))
        swipeUp.direction = UISwipeGestureRecognizer.Direction.up
        self.viewSwipe.addGestureRecognizer(swipeUp)

        let swipedown = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown(_:)))
        swipedown.direction = UISwipeGestureRecognizer.Direction.down
        self.viewSwipe.addGestureRecognizer(swipedown)
    }

    @objc func swipeUp(_ gesture: UISwipeGestureRecognizer) {
        print("swiped up")
    }

    @objc func swipeDown(_ gesture: UISwipeGestureRecognizer) {
        print("swiped down")
    }
}

红色区域需要滑动

您的代码应该可以工作。如果不存在,可能与不同的手势识别器发生冲突。要解决此冲突,您可以在 swipeUpswipeDown 识别器上设置 delegate

class ViewController: UIViewController, UIGestureRecognizerDelegate 

swipeUp.delegate = self
swipeDown.delegate = self

并尝试调查此委托函数中发生了什么(使用断点或打印,由您决定):

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return false
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

如果在您尝试滑动时调用此委托函数的 none。那么你的内容可能会有不同的(即不可见的)视图。

仅在视图中添加Gesture,您还可以在手势上添加任意方向。我在这里放了一个小例子,你可以参考这段代码来解决你的问题。

右方向取1,左方向取2

示例:

import UIKit

class ViewController: UIViewController,UIGestureRecognizerDelegate{

 @IBOutlet weak var viewGray: UIView!
 @IBOutlet weak var viewRed: UIView!

 override func viewDidLoad() {
     super.viewDidLoad()

   self.navigationController?.interactivePopGestureRecognizer?.delegate = self

   let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left]
     for direction in directions {
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
        gesture.direction = direction
        gesture.delegate = self
        self.viewRed.addGestureRecognizer(gesture)
     }
  }

  @objc func handleSwipe(sender: UISwipeGestureRecognizer) {
    print(sender.direction)
  }

   func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
       return true
   }

  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
      return true
  }
}

你的代码可以工作。尝试将您的视图控制器显示为全屏,以防万一 iOS 13. 请参考以下代码。

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)