gpt4 book ai didi

ios - UIScreenEdgePanGestureRecognizer 触发多次

转载 作者:可可西里 更新时间:2023-11-01 00:59:21 24 4
gpt4 key购买 nike

我使用以下代码在带有 UIScreenEdgePanGestureRecognizer 的 WebView 中前后移动:

class FirstViewController: UIViewController {

@IBOutlet var homewebview: UIWebView!

// create left edge and right edge gesture
let leftEdgePanGesture = UIScreenEdgePanGestureRecognizer()
let rightEdgePanGesture = UIScreenEdgePanGestureRecognizer()


override func viewDidLoad() {
super.viewDidLoad()

// add target for gesture
self.leftEdgePanGesture.addTarget(self, action: #selector(FirstViewController.handleLeftEdge(_:)))
self.rightEdgePanGesture.addTarget(self, action: #selector(FirstViewController.handleRightEdge(_:)))

// set detection edge
self.leftEdgePanGesture.edges = UIRectEdge.Left
self.rightEdgePanGesture.edges = UIRectEdge.Right

// add gesture into view
self.view.addGestureRecognizer(self.leftEdgePanGesture)
self.view.addGestureRecognizer(self.rightEdgePanGesture)
}
func handleLeftEdge( gesture: UIScreenEdgePanGestureRecognizer ) {
homewebview.goBack()
}

// perform operation when right edge gesture detected
func handleRightEdge( gesture: UIScreenEdgePanGestureRecognizer ) {
homewebview.goForward()
}
}

我的问题是它触发了多次。它不会后退/前进一个网页,它会后退/前进 ca。 5 个网页。

有什么解决办法吗?或者我必须使用 UISwipeGestureRecognizer。我不想使用 UISwipeGestureRecognizer,因为它的 react 不如 UIScreenEdgePanGestureRecognizer 可靠。 UISwipeGestureRecognizer 有时“认为”我想在 WebView 中垂直滚动但没有反应。或者有解决方案吗?

最佳答案

只要状态发生变化,平移手势识别器就会调用处理程序方法。这包括它何时开始、结束以及其间的每一个 Action 。在处理程序内部,您需要检查手势识别器的状态并决定要做什么。

您希望每个手势仅触发一次代码,并且您希望仅在触摸移动超过阈值量时执行此操作。

class ViewController: UIViewController {

let leftEdgePanGesture = UIScreenEdgePanGestureRecognizer()
var leftPanTriggered: Bool = false

override func viewDidLoad() {
super.viewDidLoad()

leftEdgePanGesture.addTarget(self, action: #selector(handleLeftEdge(_:)))
leftEdgePanGesture.edges = .Left
view.addGestureRecognizer(leftEdgePanGesture)
}

func handleLeftEdge(gesture: UIScreenEdgePanGestureRecognizer) {
switch gesture.state {
case .Began, .Changed:
if !leftPanTriggered {
let threshold: CGFloat = 10 // you decide this
let translation = abs(gesture.translationInView(view).x)
if translation >= threshold {
performLeftGesture()
leftPanTriggered = true
}
}

case .Ended, .Failed, .Cancelled:
leftPanTriggered = false

default: break
}
}

func performLeftGesture() {
homewebview.goBack()
}

}

关于ios - UIScreenEdgePanGestureRecognizer 触发多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633669/

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