[英]Pan gesture in UITableViewCell prevents scrolling in UITableView. How to fix it?
I have got an UITableView with a custom TableViewCell.我有一个带有自定义 TableViewCell 的 UITableView。 I use a pan gesture for recognizing the positions while moving my finger to the left and to the right.我使用平移手势来识别位置,同时将手指向左和向右移动。 On basis of the finger position I change some values in the labels in this TableViewCell.在手指 position 的基础上,我更改了 TableViewCell 中标签中的一些值。 This works really great.这真的很棒。 But suddenly I can not scroll the TableView up and down.但是突然我不能上下滚动TableView。 I already read the reason.我已经阅读了原因。 Swift can not work with two gesture recognizers at the same time. Swift 不能同时使用两个手势识别器。 And I found many examples of people how have nearly the same problem.我发现很多人的例子几乎相同。 I tried many of them but I can not fix my problem.我尝试了很多,但我无法解决我的问题。 I use Swift 5. Could you please describe a bit more precise how to fix my problem?我使用 Swift 5。您能否更准确地描述一下如何解决我的问题? Many thanks非常感谢
import UIKit
class TVCLebensmittel: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.addGestureRecognizer(gestureRecognizer)
}
@IBAction func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began {
let translation = gestureRecognizer.translation(in: self)
// Put my finger on the screen
} else if gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self)
// While moving my finger ...
} else if gestureRecognizer.state == .ended {
let translation = gestureRecognizer.translation(in: self)
// Lift finger
}
}
...
}
The solution is to insert the pan gesture to the tableview and not to the tableviewcell.解决方案是将平移手势插入到 tableview 而不是 tableviewcell。 So I can listen to the left and right pan and also the up and down movement of the tableview.所以我可以听到左右平移以及tableview的上下移动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.