简体   繁体   English

UITableViewCell 中的平移手势可防止在 UITableView 中滚动。 如何解决?

[英]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.

相关问题 如何将UITableViewCell放到UITableView的底部。 - How to drop UITableViewCell to bottom of UITableView. 如何防止UITableViewCell内部的手势干扰UITableView的滚动? - How to prevent a gesture inside a UITableViewCell from interfering with the UITableView's scrolling? 为什么UITableView将获得平移手势? - why the UITableView will get the pan gesture? 如何从 storyboard “Main”修复,但没有得到 UITableView。 xcode - How to fix from storyboard “Main”, but didn't get a UITableView.' xcode 如何基于UITableView滚动动态调整UITableViewCell的大小? - How to dynamically resize UITableViewCell based on UITableView scrolling? UITableView滚动时如何维护UITableViewCell状态? - How to maintain UITableViewCell state when UITableView scrolling? 尝试将平移手势应用于UITableViewCell但失败了,为什么? - Trying to apply pan gesture to UITableViewCell but failed, why? UITableView内的UICollecionView。 如何处理选择? - UICollecionView inside UITableView. How to handle selections? UITableViewcell和UITableVIew上的手势识别器 - gesture recognizers on both UITableViewcell and UITableVIew 滚动到UITableView的底部(UITableView在UITableViewCell中) - Scrolling to bottom of UITableView (UITableView is in a UITableViewCell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM