gpt4 book ai didi

ios - 删除 UILongPressGesture 默认 Action

转载 作者:行者123 更新时间:2023-11-30 12:34:57 26 4
gpt4 key购买 nike

我有一个设置,允许用户打开和关闭 UITableView 单元格上的 UILongPressGesture。我有一个简单的 bool 值,我正在检查是否应该添加或删除手势。

    // Gesture Recognizer
let longPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressDetected(_:)))

if(options?.alphaOrder == false){
self.tableView.addGestureRecognizer(longPress)
}
else{
self.tableView.removeGestureRecognizer(longPress)
}

长按方法

func longPressDetected(_ sender: Any){

let longPress:UILongPressGestureRecognizer = sender as! UILongPressGestureRecognizer
let state:UIGestureRecognizerState = longPress.state

let location:CGPoint = longPress.location(in: self.tableView) as CGPoint
let indexPath = self.tableView.indexPathForRow(at: location)

var snapshot:UIView!
var sourceIndexPath:NSIndexPath?

if (holderView) != nil{
snapshot = holderView
}

if (beginningIndexPath) != nil{
sourceIndexPath = beginningIndexPath
}


switch(state){

case UIGestureRecognizerState.began:
if let test = indexPath{
sourceIndexPath = indexPath! as NSIndexPath
beginningIndexPath = sourceIndexPath
let cell:UITableViewCell = self.tableView.cellForRow(at: test)!
snapshot = self.customSnapShotFrom(view: cell)
holderView = snapshot

var center:CGPoint = cell.center
snapshot.center = center
snapshot.alpha = 0.0
self.tableView.addSubview(snapshot)

UIView.animate(withDuration: 0.25, animations:{
center.y = location.y
snapshot.center = center
snapshot.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
snapshot.alpha = 0.98
cell.alpha = 0.0
},completion:{ _ in
cell.isHidden = true})
}
break

case UIGestureRecognizerState.changed:
// print("changed")
var center:CGPoint = snapshot.center
center.y = location.y
snapshot.center = center

if let test = indexPath {

let bool = indexPath!.elementsEqual(beginningIndexPath as IndexPath)

if !bool {

self.updatePriorities(draggedOverIndexPath: test as NSIndexPath)

}
}
default:
// print("finished")
let cell:UITableViewCell = self.tableView.cellForRow(at: sourceIndexPath as! IndexPath)!
cell.isHidden = false
cell.alpha = 0.0


UIView.animate(withDuration: 0.25, animations: {


snapshot.center = cell.center;
snapshot.transform = .identity
snapshot.alpha = 0.0;
cell.alpha = 1.0;

}, completion: { _ in

sourceIndexPath = nil
snapshot.removeFromSuperview()
snapshot = nil
})

}
}

func customSnapShotFrom(view:UIView) -> UIView {

let snapshot:UIView = view.snapshotView(afterScreenUpdates: true)!
snapshot.layer.masksToBounds = false;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSize(width: 2.0, height: 2.0);
snapshot.layer.shadowRadius = 4.0;
snapshot.layer.shadowOpacity = 1.0;

return snapshot;
}


func updatePriorities(draggedOverIndexPath:NSIndexPath) {
let firstItem: Person = fetchedResultsController.object(at: beginningIndexPath as IndexPath)
let secondItem: Person = fetchedResultsController.object(at: draggedOverIndexPath as IndexPath)

firstItem.priority = Int32(draggedOverIndexPath.row)
secondItem.priority = Int32(beginningIndexPath.row)

beginningIndexPath = draggedOverIndexPath

coreDataStack.saveContext()

}

这有效,但我注意到即使手势被删除,我仍然可以长按表格单元格并移动它。它不会重新排列表格单元格,但我想阻止这种情况发生。

最佳答案

这里有一些半伪代码,展示了我推荐的内容。这更好,因为每次 viewWillAppear 都会更新 GestureRecognizer。

class CustomViewController: UIViewController {
var longPress: UILongPressGestureRecognizer

required init() {
super.init(nibName: nil, bundle: nil)
longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressDetected(_:)))
self.tableView.addGestureRecognizer(longPress)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
longPress.isEnabled = (options?.alphaOrder == false)
}


func longPressDetected(_ sender: Any) {
...existing method...
}
}

关于ios - 删除 UILongPressGesture 默认 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001348/

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