在桌面视图中点击两个动作!
我有个关于在桌面上敲击的问题。我可以在抽头上设置辅助动作吗? 1.点击(默认)。2.我点击并保持选定的细胞2-3秒,并执行替代行动。
发布于 2018-03-26 07:45:21
您可以,您需要在您的UILongPressGestureRecognizer中添加一个cell.contentView并处理该事件,您的1个事件“普通Tap事件”将由didSelectRowAtIndexPath默认方法触发,而hold事件将由UILongPressGestureRecognizer触发。
单元格实现的示例
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示例实现
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")
}
}https://stackoverflow.com/questions/49486202
复制相似问题