gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:28:23 24 4
gpt4 key购买 nike

我提出了一个 View Controller ,其中有一个 subview 。我在 subview 上添加了滑动手势。没有调用滑动手势。相反,呈现的 View Controller 正试图关闭自己,即下降。如何覆盖滑动手势以被 subview 而不是 super View 识别。
当我在静态 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")
}
}

红色区域需要滑动

screenshot

最佳答案

添加 Gesture仅在 View 中,您还可以在手势上添加任何方向。这里我放了一个小例子,你可以引用这个代码来解决你的问题。

右方向取 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
}
}

enter image description here

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com