gpt4 book ai didi

Swift:表格 View 单元上的 UIPanGesture 不起作用

转载 作者:行者123 更新时间:2023-11-30 11:23:32 27 4
gpt4 key购买 nike

嘿嘿,您好,我想在表格 View 单元格中实现手势,但它不起作用,这是我的代码:

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
}
}

我是否必须修复函数 TableView 中的某些内容:

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!)
}

最佳答案

这是代码示例。 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

如果您有任何疑问,请提出)

关于Swift:表格 View 单元上的 UIPanGesture 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51013566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com