我介绍了一个视图控制器,并且其中有一个子视图。我在子视图上添加了滑动手势。滑动手势未调用。取而代之的是,所提供的视图控制器试图自行关闭,即关闭。如何覆盖要由子视图而不是 super 视图识别的滑动​​手势。
当我在静态视图控制器上实现滑动时,它可以按预期工作。

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

红色区域需要刷卡

ios - 当前 View  Controller 内的 subview 上未调用滑动手势-LMLPHP

最佳答案

仅在视图中添加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 - 当前 View  Controller 内的 subview 上未调用滑动手势-LMLPHP

关于ios - 当前 View Controller 内的 subview 上未调用滑动手势,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60090877/

10-12 04:35
查看更多