gpt4 book ai didi

ios - 关于在 Swift 中触摸一次屏幕时识别手势的问题

转载 作者:行者123 更新时间:2023-12-01 18:03:43 25 4
gpt4 key购买 nike

你好,我写了一个代码,使用手势识别来翻书页或在触摸时将书的屏幕更改为全屏。

fileprivate func setupTap() {
let tap = UITapGestureRecognizer(target: self, action: #selector(webViewTapped))
tap.numberOfTapsRequired = 1
tap.delegate = self
webView?.addGestureRecognizer(tap)
webView?.isUserInteractionEnabled = true
}

@objc func webViewTapped() {
setupHiddenView()
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

let prePagePoint = gestureRecognizer.location(in: self.view)
let nextPagePoint = gestureRecognizer.location(in: self.view)
let fullscreenPoint = gestureRecognizer.location(in: self.view)

if prePageTapView.frame.contains(prePagePoint) {
userDefaults.set("p", forKey: UserDefaults.Keys.pageTapped)
} else if nextPageTapView.frame.contains(nextPagePoint) {
userDefaults.set("n", forKey: UserDefaults.Keys.pageTapped)
} else if fullscreenTapView.frame.contains(fullscreenPoint) {
userDefaults.set("f", forKey: UserDefaults.Keys.pageTapped)
}
return true
}
fileprivate func setupHiddenView() {
if userDefaults.string(forKey: UserDefaults.Keys.pageTapped) == "f" {

print("FULL SCREEN MODE.")

} else if userDefaults.string(forKey: UserDefaults.Keys.pageTapped) == "p" {
print("Previous Page")
//code
} else if userDefaults.string(forKey: UserDefaults.Keys.pageTapped) == "n" {
print("Next Page")
//code
}
}


当我第一次进入看书的 View 时,触摸一次时只发生一次触摸事件。

但问题是,您可以免费观看奖励广告和阅读书籍。
如果从打赏广告界面再次进入看书界面,一次触摸会发生两次触摸事件。

奖励广告观看5次后,触摸一次,会发生5次触摸事件。好像每次进入奖励广告界面, gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer)-> Bool功能好像有问题。

我能做些什么来防止这种情况发生?

最佳答案

如果您想获取手势识别器的位置,则不应从委托(delegate)中获取它。相反,您应该从 Action 中传递手势,如下所示。

    @objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {

let prePagePoint = sender.location(in: self.view)
let nextPagePoint = sender.location(in: self.view)
let fullscreenPoint = sender.location(in: self.view)

if prePageTapView.frame.contains(prePagePoint) {
userDefaults.set("p", forKey: UserDefaults.Keys.pageTapped)
} else if nextPageTapView.frame.contains(nextPagePoint) {
userDefaults.set("n", forKey: UserDefaults.Keys.pageTapped)
} else if fullscreenTapView.frame.contains(fullscreenPoint) {
userDefaults.set("f", forKey: UserDefaults.Keys.pageTapped)
}

// Handle event
}

然后将其设置为
   let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
tap.numberOfTapsRequired = 1
tap.delegate = self
webView?.addGestureRecognizer(tap)

关于ios - 关于在 Swift 中触摸一次屏幕时识别手势的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667091/

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