私はここで初めてジェスチャーで作業しています。私のアプローチが間違っているか、より良い解決策であれば教えてください。TapViewジェスチャーでCollectionViewセルの位置をリセット
私はコレクションビューのセルをスワイプして削除しようとしています。ちょうどUITableviewの削除機能と同じです。削除は正常に動作します。今、私が欲しいものを私はのviewDidLoadとを更新しました。このコード をしようとして/使用しています(テーブルビューの削除行の機能と同じような)私は、セルをスワイプしてCOllectionView上の任意の場所をタップすると、それが元の位置にスワイプする必要があり、
です私はself.collectionView.performBatchUpdatesが、そのない滑らかなアニメーションで元に戻し、セルの位置を設定することができていますので、ここでイベント
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
self.view.addGestureRecognizer(tap)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CustomCell
Cell.backgroundColor = UIColor.white
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(delete(sender:)))
leftSwipe.direction = UISwipeGestureRecognizerDirection.left
Cell.addGestureRecognizer(leftSwipe)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
Cell.addGestureRecognizer(tap)
Cell.deleteButton.addTarget(self, action: #selector(DeleteCell(sender:)), for: .touchUpInside)
}
func tapped(_ recognizer: UITapGestureRecognizer) {
// self.collectionView.performBatchUpdates({
//self.collectionView.reloadSections(NSIndexSet(index: 0) as IndexSet)
//}, completion: nil)
let point = recognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: point)
let cell = self.collectionView.cellForItem(at: indexPath!)
UIView.animate(withDuration: 0.4) {
cell?.contentView.frame = CGRect(x: 0, y: 0, width: (cell?.contentView.frame.width)!, height: (cell?.contentView.frame.height)!)
}
}
func delete(sender: UISwipeGestureRecognizer){
let cell = sender.view as! CustomCell
UIView.animate(withDuration: 0.4) {
cell.contentView.frame = CGRect(x: -90, y: 0, width: cell.contentView.frame.width, height: cell.contentView.frame.height)
}
}
func DeleteCell(sender : AnyObject){
let cell = sender.superview as! CustomCell
let i = self.collectionView.indexPath(for: cell)!.item
let indexpath = self.collectionView.indexPath(for: cell)
let array : NSMutableArray = []
self.collectionView.performBatchUpdates({
self.userArray.remove(at: i)
array.add(indexpath!)
self.collectionView.deleteItems(at:array as! [IndexPath])
}, completion: nil)
}
class CustomCell: UICollectionViewCell {
let deleteButton: UIButton = {
let deleteBtn = UIButton()
deleteBtn.setImage(UIImage(named: "red"), for: .normal)
deleteBtn.contentMode = .scaleAspectFit
return deleteBtn
}()
}
をタップ。私は
UIView.animate(withDuration: 0.4) {
cell.contentView.frame = CGRect(x: 0, y: 0, width: cell.contentView.frame.width, height: cell.contentView.frame.height)
}
を使用してみましたが、それはどこか他の任意の他の細胞、スワイプセルがタップた場合にのみ機能していませんか。どんな提案も参考になるでしょう!
ありがとう、ジャコラック!私はそれを試したと同じです。選択したセルをタップすると、セルが戻るだけです。 – iUser
これをどのクラスに追加していますか? – Jacolack
申し訳ありませんが、ここであなたが求めているクラスはありませんでした。上記のコードを更新しました。それを見てください。 – iUser