gpt4 book ai didi

ios - 向 UIScrollView 添加垂直两指滑动手势

转载 作者:搜寻专家 更新时间:2023-10-31 22:38:15 25 4
gpt4 key购买 nike

根据 How to add a vertical swipe gesture to iPhone app for all screens ,我向 window 添加了一个双指向下滑动手势,这在整个应用程序的普通页面上都可以正常工作。但它在页面上失败了 UIScrollView(如 UITableViewController)。当我在 UIScrollView用两根手指向下滑动时,它只是像往常一样滚动。如果我从 UIScrollView 上方的 UINavigationBar 滑动,它再次正常工作。

理想的结果是我可以正常用一根手指滚动tableview,不用滚动tableview用两根手指滑动页面调用一些方法。这在 TweetBot 中用于切换黑暗模式,效果很好。

根据 Apple 文档:Using Responders and the Responder Chain to Handle Events ,我想我了解响应链是如何工作的,所以我想请 UIScrollView 忽略两指滑动手势,以便它可以将此事件传递给 UIWindow。但我不知道如何:

我试图实现来自 Apple 的 UIGestureRecognizerDelegatefunc gestureRecognizer(_:, shouldRequireFailureOf otherGestureRecognizer:) document ,或通过继承 UITableView 覆盖 gestureRecognizerShouldBegin(_)。但都没有用。

欢迎任何解决方案或建议。

更新 - 最终解决方案

我简化了 joern 的解决方案,这里是。

AppDelegate

// It's not necessary to keep a reference to gesture unless you want to do something further.
lazy var gesture: UIPanGestureRecognizer = {
let gesture = UIPanGestureRecognizer(target: self, action: #selector(twoFingerDidSwipe(recognizer:)))
gesture.minimumNumberOfTouches = 2
gesture.maximumNumberOfTouches = 2
return gesture
}()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
gesture.delegate = AppDelegate
window.addGestureRecognizer(gesture)
return true
}

@objc func twoFingerDidSwipe(recognizer: UIPanGestureRecognizer) {
let swipeThreshold: CGFloat = 50

if recognizer.state == .changed { // 1
switch recognizer.translation(in: window).y { // 2
case ...(-swipeThreshold):
print("Swipe Up")
recognizer.state = .cancelled // 3
case swipeThreshold...:
print("Swipe Down")
recognizer.state = .cancelled
default:
break
}
}
}

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

关于与 joern 解决方案差异的一些解释:

  1. 只关注recognizer.state == .change
  2. recognizer.translation(in:) 的参数应该是 window 这样我就不能对特定的 VC 做任何事情。
  3. 此识别器触发某些方法后,应将其取消,以防止继续触发它们。

而且没有必要对任何特定的 VC 做任何事情。

有了这个,双指滑动手势在普通 VC 和滚动 VC 上都可以正常工作。

最佳答案

当您将 UISwipeGestureRecognizer 添加到您的窗口时,保留对它的引用,以便您稍后可以通过 AppDelegate 访问它:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var twoFingerSwipeDownRecognizer: UISwipeGestureRecognizer?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

let twoFingerSwipeDownRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(didRecognizeTwoFingerSwipeDown))
twoFingerSwipeDownRecognizer.numberOfTouchesRequired = 2
twoFingerSwipeDownRecognizer.direction = .down
window?.addGestureRecognizer(twoFingerSwipeDownRecognizer)
self.twoFingerSwipeDownRecognizer = twoFingerSwipeDownRecognizer

return true
}

@objc func didRecognizeTwoFingerSwipeDown(recognizer: UISwipeGestureRecognizer) {
print("SWIPE DOWN")
}
}

然后,在包含 UITableView(或 UIScrollView)的 UIViewController 中,您必须调用 require(toFail:) 在 UITableView 的平移手势识别器上:

func enableTwoFingerSlideDown() {
guard
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let twoFingerGestureRecognizer = appDelegate.twoFingerSwipeDownRecognizer
else {
return
}
tableView.panGestureRecognizer.require(toFail: twoFingerGestureRecognizer)
}

现在,两根手指向下的手势适用于 UITableView

更新

因为滑动是一个离散的手势,所以上面的解决方案可能不是完美的解决方案。当您用一根 手指缓慢向下滚动时,您会注意到UITableView 不会立即向下滚动。有一个短暂的延迟,因为 UITableView 的 UIPanGestureDelagate 必须等到(双指)SwipeDelegate 失败。这需要一些时间。

更好的解决方案可能是使用 UIPanGestureRecognizer 来识别双指平移,然后在用户使用两根手指平移时禁用 UITableView 上的滚动。

可以这样实现:

在您的 AppDelegate 中:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var twoFingerPanRecognizer: UIPanGestureRecognizer?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

let twoFingerSwipeDownRecognizer = UIPanGestureRecognizer(target: self, action: #selector(didRecognizeTwoFingerPan))
twoFingerSwipeDownRecognizer.minimumNumberOfTouches = 2
twoFingerSwipeDownRecognizer.maximumNumberOfTouches = 2
twoFingerPanRecognizer?.delegate = self
window?.addGestureRecognizer(twoFingerSwipeDownRecognizer)
self.twoFingerPanRecognizer = twoFingerSwipeDownRecognizer

return true
}

@objc func didRecognizeTwoFingerPan(recognizer: UIPanGestureRecognizer) {
let tableView = recognizer.view as? UITableView
switch recognizer.state {
case .began:
tableView?.isScrollEnabled = false
case .changed:
let swipeThreshold: CGFloat = 50
switch recognizer.translation(in: nil).y {
case ...(-swipeThreshold):
print("Swipe UP")
recognizer.isEnabled = false
case swipeThreshold...:
print("Swipe DOWN")
recognizer.isEnabled = false
default:
break
}
case .cancelled, .ended, .failed, .possible:
tableView?.isScrollEnabled = true
recognizer.isEnabled = true
}
}
}

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

在您的 ViewController 中:

func enableTwoFingerSlideDown() {
guard
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let twoFingerGestureRecognizer = appDelegate.twoFingerPanRecognizer
else {
return
}
tableView.addGestureRecognizer(twoFingerGestureRecognizer)
}

您必须从 AppDelegate 中获取 UIPanGestureRecognizer 并将其添加到 UITableView。否则这是行不通的。请记住在关闭此 UIViewController 之前将其添加回 UIWindow

使用此解决方案,UITableView 的正常滚动行为保持不变。

关于ios - 向 UIScrollView 添加垂直两指滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908002/

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