Swift CollectionView 使用滑动手势删除项目
Swift CollectionView Remove Items with swipe gesture
我有一个水平滚动的集合视图。
我正在寻找使用向上或向下滑动手势删除项目的简洁方法。
重新排列元素也很棒,但目前删除更重要。
我找到了一些 Obj-C 文档,但是,由于我对 swift Obj-C 还是个新手,所以对我来说太多了。
最近几天我一直在处理同样的情况。
这是我对 swift 所做的。我检查了 Michael 的 link 并做了一些研究...
所以..
添加这个
let cSelector = Selector("reset:")
let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector )
UpSwipe.direction = UISwipeGestureRecognizerDirection.Up
cell.addGestureRecognizer(UpSwipe)
至
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
然后定义您的选择器,它实际上会从您的数组中删除滑动的项目,然后重新加载您的集合视图。
func reset(sender: UISwipeGestureRecognizer) {
let cell = sender.view as! UICollectionViewCell
let i = self.favoritesCV.indexPathForCell(cell)!.item
favoritesInstance.favoritesArray.removeAtIndex(i) //replace favoritesInstance.favoritesArray with your own array
self.favoritesCV.reloadData() // replace favoritesCV with your own collection view.
}
您也可以通过在单元格中使用多个视图来实现。
这是我的代码。先用三视图。
示例:-
@IBOutlet weak var YOURVIEW: UIView!
@IBOutlet weak var edit: UIView!
@IBOutlet weak var delete: UIView!
现在对 YOURVIEW
的前导和尾部进行布局
@IBOutlet weak var YOURLEADING: NSLayoutConstraint!
@IBOutlet weak var YOURTRAILING: NSLayoutConstraint!
将其添加到覆盖 func awakeFromNib()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = .left
self.YOURTOPVIEW.addGestureRecognizer(swipeLeft
)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.YOURTOPVIEW.addGestureRecognizer(swipeRight)
现在把这段代码写在class body
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .left:
self.animate()
self.YOURLEADING.constant = -100
self.YOURTRAILING.constant = 100
// YOUR OTHER ACTIONS HERE
case .right:
self.animate()
self.YOURLEADING.constant = 100
self.YOURTRAILING.constant = -100
// YOUR OTHER ACTIONS HERE
default:
break
}
}
}
也做一个函数来显示动画
func animate()
{
UIView.animate(withDuration: 1,
delay: 0.0,
animations: { () -> Void in
self.YOURTOPVIEW.frame = CGRect(x: 0, y: 0, width: self.YOURTOPVIEW.frame.width, height: self.YOURTOPVIEW.frame.height)
}, completion: { (finished: Bool) -> Void in })
}
现在手势识别器将在该特定视图上工作,并且看起来就像您正在滑动集合视图单元格。
我有一个水平滚动的集合视图。
我正在寻找使用向上或向下滑动手势删除项目的简洁方法。
重新排列元素也很棒,但目前删除更重要。
我找到了一些 Obj-C 文档,但是,由于我对 swift Obj-C 还是个新手,所以对我来说太多了。
最近几天我一直在处理同样的情况。 这是我对 swift 所做的。我检查了 Michael 的 link 并做了一些研究...
所以..
添加这个
let cSelector = Selector("reset:")
let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector )
UpSwipe.direction = UISwipeGestureRecognizerDirection.Up
cell.addGestureRecognizer(UpSwipe)
至
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
然后定义您的选择器,它实际上会从您的数组中删除滑动的项目,然后重新加载您的集合视图。
func reset(sender: UISwipeGestureRecognizer) {
let cell = sender.view as! UICollectionViewCell
let i = self.favoritesCV.indexPathForCell(cell)!.item
favoritesInstance.favoritesArray.removeAtIndex(i) //replace favoritesInstance.favoritesArray with your own array
self.favoritesCV.reloadData() // replace favoritesCV with your own collection view.
}
您也可以通过在单元格中使用多个视图来实现。 这是我的代码。先用三视图。
示例:-
@IBOutlet weak var YOURVIEW: UIView!
@IBOutlet weak var edit: UIView!
@IBOutlet weak var delete: UIView!
现在对 YOURVIEW
的前导和尾部进行布局@IBOutlet weak var YOURLEADING: NSLayoutConstraint!
@IBOutlet weak var YOURTRAILING: NSLayoutConstraint!
将其添加到覆盖 func awakeFromNib()
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = .left
self.YOURTOPVIEW.addGestureRecognizer(swipeLeft
)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.YOURTOPVIEW.addGestureRecognizer(swipeRight)
现在把这段代码写在class body
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .left:
self.animate()
self.YOURLEADING.constant = -100
self.YOURTRAILING.constant = 100
// YOUR OTHER ACTIONS HERE
case .right:
self.animate()
self.YOURLEADING.constant = 100
self.YOURTRAILING.constant = -100
// YOUR OTHER ACTIONS HERE
default:
break
}
}
}
也做一个函数来显示动画
func animate()
{
UIView.animate(withDuration: 1,
delay: 0.0,
animations: { () -> Void in
self.YOURTOPVIEW.frame = CGRect(x: 0, y: 0, width: self.YOURTOPVIEW.frame.width, height: self.YOURTOPVIEW.frame.height)
}, completion: { (finished: Bool) -> Void in })
}
现在手势识别器将在该特定视图上工作,并且看起来就像您正在滑动集合视图单元格。