在 Swift 3 中单击和双击 UITableViewCell

Posted

技术标签:

【中文标题】在 Swift 3 中单击和双击 UITableViewCell【英文标题】:Single and double taps on UITableViewCell in Swift 3 【发布时间】:2017-04-01 06:22:40 【问题描述】:

我在 TableViewCell 上有故事板 segue,我用它在didSelectRowAt 方法中单击单元格时转移到另一个 VC。现在我双击TapGestureRecognizer 来处理对单元格的轻敲。问题是单击时,segue 正在执行,而双击不起作用。双击可以很好地单击单元格外。到目前为止,有可能用我的代码以某种方式解决这个问题吗?或者我需要删除 segue 并分别处理单击和双击。 感谢您的任何建议

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
    doubleTap.numberOfTapsRequired = 2
    view.addGestureRecognizer(doubleTap)


func handleDoubleTap(recognizer: UIGestureRecognizer) 
    let p = recognizer.location(in: tableView)

    let indexPath = tableView.indexPathForRow(at: p)

    if let _ = indexPath 
        tableView.deselectRow(at: indexPath!, animated: true)
        update(index: (indexPath?.row)!, isFinished: true)
    

    print ("doubke")



override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if (segue.identifier == "showSingleTask") 
        if let indexPath = tableView.indexPathForSelectedRow 
            let nav = segue.destination as! UINavigationController
            let destinationVC = nav.topViewController as! ShowTaskVC
            destinationVC.singleTask = tasks[indexPath.row]
        
    


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

    tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    self.selectedTask = tasks[indexPath.row]

【问题讨论】:

所以我有一个问题.. 你想双击单元格以便它调用 segue 吗?也许提供更多细节会有所帮助。 禁用默认点击单元格并在单元格上添加单击手势,就像添加双击一样。 @JArango 我想双击单元格以调用 handleDoubleTap 方法并单击单元格以执行 segue,我想知道是否可以使用 didSelectRowAt 方法或我需要添加单击手势如上所述 【参考方案1】:

详情

Xcode 10.2.1 (10E1001)、Swift 5

解决方案

protocol MultiTappableDelegate: class 
    func singleTapDetected(in view: MultiTappable)
    func doubleTapDetected(in view: MultiTappable)


class ThreadSafeValue<T> 
    private var _value: T
    private lazy var semaphore = DispatchSemaphore(value: 1)
    init(value: T)  _value = value 
    var value: T 
        get 
            semaphore.signal(); defer  semaphore.wait() 
            return _value
        
        set(value) 
            semaphore.signal(); defer  semaphore.wait() 
            _value = value
        
    


protocol MultiTappable: UIView 
    var multiTapDelegate: MultiTappableDelegate?  get set 
    var tapCounter: ThreadSafeValue<Int>  get set 


extension MultiTappable 
    func initMultiTap() 
        if let delegate = self as? MultiTappableDelegate  multiTapDelegate = delegate 
        let tap = UITapGestureRecognizer(target: self, action: #selector(UIView.multitapActionHandler))
        addGestureRecognizer(tap)
    

    func multitapAction() 
        if tapCounter.value == 0 
            DispatchQueue.global(qos: .utility).async 
                usleep(250_000)
                DispatchQueue.main.async  [weak self] in
                    guard let self = self else  return 
                    if self.tapCounter.value > 1 
                        self.multiTapDelegate?.doubleTapDetected(in: self)
                     else 
                        self.multiTapDelegate?.singleTapDetected(in: self)
                    
                    self.tapCounter.value = 0
                
            
        
        tapCounter.value += 1
    


private extension UIView 
    @objc func multitapActionHandler() 
        if let tappable = self as? MultiTappable  tappable.multitapAction() 
    

用法

class MyView: UIView, MultiTappable 
    weak var multiTapDelegate: MultiTappableDelegate?
    lazy var tapCounter = ThreadSafeValue(value: 0)
    override func awakeFromNib() 
        super.awakeFromNib()
        initMultiTap()
    

完整样本

ViewController.swift

import UIKit

class ViewController: UIViewController 
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() 
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
    


extension ViewController: UITableViewDataSource 

    func numberOfSections(in tableView: UITableView) -> Int  return 1 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int  return 10 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
        cell.label.text = "\(indexPath)"
        cell.delegate = self
        return cell
    


extension ViewController: TableViewCellDelegate 
    func singleTapDetected(in cell: TableViewCell)  
        if let indexPath = tableView.indexPath(for: cell)  print("singleTap \(indexPath) ") 
    
    func doubleTapDetected(in cell: TableViewCell) 
        if let indexPath = tableView.indexPath(for: cell)  print("doubleTap \(indexPath) ") 
    

TableViewCell.swift

import UIKit

protocol TableViewCellDelegate: class 
    func singleTapDetected(in cell: TableViewCell)
    func doubleTapDetected(in cell: TableViewCell)


class TableViewCell: UITableViewCell, MultiTappable 
    weak var multiTapDelegate: MultiTappableDelegate?
    lazy var tapCounter = ThreadSafeValue(value: 0)

    @IBOutlet weak var label: UILabel!
    weak var delegate: TableViewCellDelegate?

    override func awakeFromNib() 
        super.awakeFromNib()
        initMultiTap()
    


extension TableViewCell: MultiTappableDelegate 
    func singleTapDetected(in view: MultiTappable)  self.delegate?.singleTapDetected(in: self) 
    func doubleTapDetected(in view: MultiTappable)  self.delegate?.doubleTapDetected(in: self) 

Main.storyboard

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="g2V-T0-sqD">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
        <capability name="Constraints to layout margins" minToolsVersion="6.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="***_43153530" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0"  />
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="fQm-mQ-a9u">
                                <rect key="frame" x="0.0" y="0.0"  />
                                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                <prototypes>
                                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="TableViewCell" id="nsF-ue-0bK" customClass="TableViewCell" customModule="***_43153530" customModuleProvider="target">
                                        <rect key="frame" x="0.0" y="28"  />
                                        <autoresizingMask key="autoresizingMask"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nsF-ue-0bK" id="pT6-2N-oTC">
                                            <rect key="frame" x="0.0" y="0.0"  />
                                            <autoresizingMask key="autoresizingMask"/>
                                            <subviews>
                                                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fEK-J3-oqH">
                                                    <rect key="frame" x="8" y="8"  />
                                                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                    <nil key="textColor"/>
                                                    <nil key="highlightedColor"/>
                                                </label>
                                            </subviews>
                                            <constraints>
                                                <constraint firstItem="fEK-J3-oqH" firstAttribute="leading" secondItem="pT6-2N-oTC" secondAttribute="leadingMargin" id="Vfg-Ij-f6c"/>
                                                <constraint firstItem="fEK-J3-oqH" firstAttribute="top" secondItem="pT6-2N-oTC" secondAttribute="topMargin" id="tc0-qJ-N1n"/>
                                            </constraints>
                                        </tableViewCellContentView>
                                        <connections>
                                            <outlet property="label" destination="fEK-J3-oqH" id="YBJ-tG-J5T"/>
                                        </connections>
                                    </tableViewCell>
                                </prototypes>
                            </tableView>
                        </subviews>
                        <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstItem="fQm-mQ-a9u" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="8Vy-l8-jpB"/>
                            <constraint firstItem="fQm-mQ-a9u" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="Wwr-ox-Qbd"/>
                            <constraint firstItem="fQm-mQ-a9u" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="-64" id="xJR-Uk-rbj"/>
                            <constraint firstAttribute="trailing" secondItem="fQm-mQ-a9u" secondAttribute="trailing" id="zxs-ED-Whb"/>
                        </constraints>
                    </view>
                    <navigationItem key="navigationItem" id="pLJ-Bz-NIm"/>
                    <connections>
                        <outlet property="tableView" destination="fQm-mQ-a9u" id="DhZ-jj-zmB"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="1079.2" y="137.18140929535232"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="w7e-Wj-oUR">
            <objects>
                <navigationController automaticallyAdjustsScrollViewInsets="NO" id="g2V-T0-sqD" sceneMemberID="viewController">
                    <toolbarItems/>
                    <navigationBar key="navigationBar" contentMode="scaleToFill" id="7qG-8v-S0O">
                        <rect key="frame" x="0.0" y="0.0"  />
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <nil name="viewControllers"/>
                    <connections>
                        <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="yqZ-pK-Yf3"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="tnz-x0-vDN" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="140" y="137.18140929535232"/>
        </scene>
    </scenes>
</document>

结果

【讨论】:

谢谢,它有效!即使更改某些 UI 似乎有延迟 @Vasily Bodnarchuk 对于 UICollectionView 是否也能以同样的方式工作? @MD.SazidHasanDip 是的。您可以对 UICollectionView 使用相同的想法。【参考方案2】:

在单元格类中添加以下方法:

private func addSingleAndDoubleTapGesture() 
    let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
    singleTapGesture.numberOfTapsRequired = 1
    self.addGestureRecognizer(singleTapGesture)

    let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
    doubleTapGesture.numberOfTapsRequired = 2
    self.addGestureRecognizer(doubleTapGesture)

    singleTapGesture.require(toFail: doubleTapGesture)


@objc private func handleSingleTap(_ tapGesture: UITapGestureRecognizer) 



@objc private func handleDoubleTap(_ tapGesture: UITapGestureRecognizer) 


【讨论】:

【参考方案3】:

对我来说,在 UITableViewCell 类中添加双击手势识别器就足够了。 像这样。

class TableViewCellSubclass: UITableViewCell 

    override func awakeFromNib() 
        super.awakeFromNib()
    
        let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapFunc))
        doubleTap.numberOfTapsRequired = 2
        self.addGestureRecognizer(doubleTap)
    

    @objc func doubleTapFunc() 
    

只需像往常一样使用 didSelectRowAt 方法单击即可。

【讨论】:

我同意。它工作得相当干净。双击手势的处理完全独立于将您带入didSelectRowAt 的单击,因此无需担心一个人踩到另一个人并弄乱了点击次数。使用上面的示例,我添加了protocol TableViewCellSubclassDelegate func doubleTapped(cell: TableViewCellSubclass) ,然后添加了@objc func doubleTapFunc() delegate?.doubleTapped(cell: self) 。实现这个协议的视图控制器将能够捕捉到这个双击并用它做任何它想做的事情。

以上是关于在 Swift 3 中单击和双击 UITableViewCell的主要内容,如果未能解决你的问题,请参考以下文章

单击事件和双击事件冲突问题

如何区分鼠标的单击和双击事件

如何识别iOS中的单个单击和双击视图?

Xamarin Forms:如何为图像添加单击和双击?

在 UITableViewCell 的子视图上添加单击和双击手势

UIGesture 识别器,区分单击和双击。