gpt4 book ai didi

ios - 如何防止 UITableViewCell 中的手势干扰 UITableView 的滚动?

转载 作者:行者123 更新时间:2023-11-28 14:25:35 30 4
gpt4 key购买 nike

我觉得这个问题之前已经被问过很多次了,但我已经找了好几天了,找不到有效的解决方案。在 Swift 4 中,我有一个 UITableView,其中每个 UITableViewCell 都有可触摸的 UIView。这个 UIView 有一个 UILongPressGestureRecognizer 附加到它影响它的按下状态并在按下时导航到另一个屏幕。所有行都相对靠近,不幸的是,为了启动平移手势来滚动 UITableView,您必须按下列表项,这会触发其手势识别器并立即离开页面。

最初,您无法按下 UITableViewCell 来触发滚动,所以我在 UIView 中设置了手势委托(delegate)并添加了这个函数:

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

从而导致我目前遇到的问题。这是我在 UITableViewCell 中使用的自定义 UIView 类:

import Foundation
import UIKit

protocol ItemViewDelegate: AnyObject {
func tapped()
}

class ItemView: UIView, UIGestureRecognizerDelegate {
weak var delegate: ItemViewDelegate?
var parent: UIViewController?

override init(frame: CGRect) {
super.init(frame: frame)
self.initializeGestureRecognition()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initializeGestureRecognition()
}

lazy var animator: UIViewPropertyAnimator = {
let cubicParameters = UICubicTimingParameters(controlPoint1: CGPoint(x: 0, y: 0.5), controlPoint2: CGPoint(x: 1.0, y: 0.5))
let animator = UIViewPropertyAnimator(duration: 0.1, timingParameters: cubicParameters)

animator.isInterruptible = true

return animator
}()

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

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if (type(of: otherGestureRecognizer) == UIPanGestureRecognizer.self) {
return true
}

return false
}

private func initializeGestureRecognition() {
let tapRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleTap(from:)))

tapRecognizer.minimumPressDuration = 0
tapRecognizer.delegate = self
tapRecognizer.cancelsTouchesInView = false

self.isUserInteractionEnabled = true
self.addGestureRecognizer(tapRecognizer)
}

private func tapped() {
guard let delegate = self.delegate else {
return
}

delegate.tapped()
}

override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(radius: 4)
self.addShadow(opacity: 0.06, radius: 4)
}

@objc func handleTap(from recognizer: UITapGestureRecognizer) {
switch recognizer.state {
case .began:
if animator.isRunning {
animator.stopAnimation(true)
}

animator.addAnimations {
self.transform = CGAffineTransform(scaleX: 0.98, y: 0.98)
}

animator.startAnimation()
case .ended:
if animator.isRunning {
animator.stopAnimation(true)
}

animator.addAnimations {
self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}

animator.startAnimation()

self.tapped()
default:
break
}
}
}

我认为 shouldRequireFailureOf 覆盖肯定会起作用,因为这就是 Apple 的文档似乎建议的,但我现在已经尝试了几乎所有方法并且正在拔头发。任何帮助,将不胜感激。谢谢!

最佳答案

您可以在包含 tableView 的 VC 中定义此 var

var isScrolling = false

//

class ViewController: UIViewController , UIScrollViewDelegate

//

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

isScrolling = true

}

func scrollViewDidScroll(_ scrollView: UIScrollView) {

isScrolling = false

}

//

在单元格内部读取这个值

@objc func handleTap(from recognizer: UITapGestureRecognizer) {

if parentVc.isScrolling { return }

.....
}

关于ios - 如何防止 UITableViewCell 中的手势干扰 UITableView 的滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617365/

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