嘿, 您好,我想在tableview单元格中实现一个手势,但是它在我的代码中不起作用:
var panGesture: UIPanGestureRecognizer?
override func awakeFromNib() {
super.awakeFromNib()
panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture?.delaysTouchesBegan = false
panGesture?.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture!)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@objc func detectPan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.vc?.view)
switch recognizer.state {
case .began:
self.startingConstant = self.constantCenter.constant
case .changed:
self.constantCenter.constant = self.startingConstant + translation.x
case .ended:
if translation.x > 0 {
constantCenter.isActive = false
constanteGauche.isActive = false
constanteDroite.isActive = true
}else{
constantCenter.isActive = false
constanteGauche.isActive = true
constanteDroite.isActive = false
print("ok")
}
UIView.animate(withDuration: 0.2) {
self.vc?.view.layoutIfNeeded()
}
constantCenter.constant = separatorView.center.x - (self.vc?.view.center.x)!
constantCenter.isActive = true
constanteGauche.isActive = true
constanteDroite.isActive = true
default:
break
}
}
我是否必须在函数表视图中修复某些问题?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//On lien le TableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? FeedCell
cell?.selectionStyle = .none
cell?.vc = self
cell?.delegate = self
cell?.view(post: FeedPostController.posts[indexPath.row], user: FeedPostController.users[indexPath.row]); //On passe l'objet a l'index i a la fonction view du TableViewCell pour l'affichage
return cell!;
}
我尝试了很多可能性,但没有任何效果,在uitableviewcell中我需要手势的功能,因为我使用了单元格中的约束
更新解决方案
override func awakeFromNib() {
super.awakeFromNib()
separatorView.isUserInteractionEnabled = true //THIS LINE ITS VERY IMPORTANT !!!!!!!!
panGesture = UIPanGestureRecognizer(target: self, action: #selector(detectPan(recognizer:)))
panGesture?.delaysTouchesBegan = false
panGesture?.delaysTouchesEnded = false
separatorView.addGestureRecognizer(panGesture!)
}
答案 0 :(得分:0)
这是代码示例。 100%有效。希望会有所帮助。
import UIKit
private let cellID = "cellID"
class ViewController: UIViewController {
// Setup Table View
private lazy var tableView: UITableView = {
let rect: CGRect = view.bounds
let tableView = UITableView(frame: rect, style: .grouped)
tableView.separatorStyle = .singleLine
tableView.backgroundColor = .white
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
// If you need to PAN the tableView, uncomment this line
// tableView.addGestureRecognizer(self.panGesture)
return tableView
}()
// Setup panGesture
private lazy var panGesture: UIPanGestureRecognizer = {
let pan = UIPanGestureRecognizer(target: self, action: #selector(didPan(sender:)))
// Set delegate
pan.delegate = self
return pan
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
cell.backgroundColor = .red
// Add panGesture to first cell for example
if indexPath.section == 0, indexPath.row == 0 {
cell.addGestureRecognizer(self.panGesture)
cell.backgroundColor = .lightGray
cell.textLabel?.text = "You can drag a table for this cell"
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
}
extension ViewController: UIGestureRecognizerDelegate {
@objc fileprivate func didPan(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: view)
switch sender.state {
case .began:
print("Began")
case .changed:
print("Canged \(translation.y)")
// This is just example to show you how that works. Dragging tableView.
tableView.frame.origin.y = translation.y
case .ended:
print("Ended")
default:
break
}
}
}
链接短片视频,其在设备上的工作方式:https://www.dropbox.com/s/1lorvamynbwvyv7/TJCQ1216.MP4?dl=0
如果您有任何问题,只需询问)