首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tableview中进行点击的两个操作

在tableview中进行点击的两个操作
EN

Stack Overflow用户
提问于 2018-03-26 07:37:21
回答 1查看 198关注 0票数 3

在桌面视图中点击两个动作!

我有个关于在桌面上敲击的问题。我可以在抽头上设置辅助动作吗? 1.点击(默认)。2.我点击并保持选定的细胞2-3秒,并执行替代行动。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-26 07:45:21

您可以,您需要在您的UILongPressGestureRecognizer中添加一个cell.contentView并处理该事件,您的1个事件“普通Tap事件”将由didSelectRowAtIndexPath默认方法触发,而hold事件将由UILongPressGestureRecognizer触发。

单元格实现的示例

代码语言:javascript
复制
import UIKit

class LongPressTableViewCell: UITableViewCell {

    var longPressGesture : UILongPressGestureRecognizer?
    var longPressClosure : (()->Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func setupWithClosure(closure:@escaping (()->Void)) {
        self.longPressClosure = closure
        if(longPressGesture == nil) {
            longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gesture:)))
            longPressGesture!.minimumPressDuration = 2
            self.contentView.addGestureRecognizer(longPressGesture!)
        }
    }



    @objc func longPressAction(gesture:UILongPressGestureRecognizer) {
        if (gesture.state == UIGestureRecognizerState.began){
                self.longPressClosure?()
        }
     }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

TableView DataSource && Delegate示例实现

代码语言:javascript
复制
extension ViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "LongPressTableViewCell", for: indexPath) as? LongPressTableViewCell{
            cell.setupWithClosure {
                //LongPress action
                debugPrint("LongPress")
            }
            return cell
        }

        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        debugPrint("Tap Action")
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49486202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档