我正在尝试在我的 collectionViewCell 中实现 UISwipeGestureRecognizer,因此当您向左滑动时,单元格会消失。我想要实现的(我找不到方法)是为滑动设置动画,所以当我向左滑动单元格时,它会消失并带有淡入淡出效果。这是我在方法 cellForItemAtindexPath
let cSelector = #selector(reset(sender:))
let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)
UpSwipe.direction = UISwipeGestureRecognizerDirection.left
cell.addGestureRecognizer(UpSwipe)
方法
func reset(sender: UISwipeGestureRecognizer) {
let cell = sender.view as! UICollectionViewCell
let i = self.collectionView?.indexPath(for: cell)!.item
self.messages.remove(at: i!)
self.collectionView?.reloadData()
}
谢谢!!!
编辑: 我想我找到了一种最简单的方法,但我遇到了一些麻烦。我尝试在单元格中实现 UIPanGestureRecognizer。这是它的样子......
cellForItemAt
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(gestureRecognizer:)))
cell.addGestureRecognizer(gestureRecognizer)
方法
func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began {
// When the drag is first recognized, you can get the starting coordinates here
}
if gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// Translation has both .x and .y values
if translation.x == translation.x - 100 {
//Method i putted before
reset(sender: gestureRecognizer)
}
//print(translation.x, translation.y)
}
}
我正在尝试定位单元格的坐标,所以当它位于单元格左侧的某个点时,单元格开始播放某种淡入淡出的动画,然后消失。
有什么帮助吗???谢谢!!!
最佳答案
有两种选择可以实现您的目标。
- 创建自定义布局
- 通过滑动手势使用 UIView
创建自定义布局
您可以根据您选择的动画创建自定义布局。 Here是引用。你只需要修改它的动画。
通过滑动手势使用 UIView
按照以下步骤操作
- 在 CollectionView Cell 中添加 UIView(var name -
swipeView
)并为 UIView 设置背景颜色。 - 将滑动手势(向左和/或向右)添加到 swipeView
- 使用不同状态的滑动手势(开始、拖动、结束)处理 View 滑动以及用户的拖动操作。
- 当滑动手势结束时,将
swipeView
推出带有动画的单元格(设置它们的 x 位置,使其可以超出单元格框架的边界) - 从数组中删除元素并重新加载 Collection View 。
我希望,通过上面的逻辑你可以做你想做的,你可能不需要现成的代码。
关于ios - UISwipeGestureRecognizer 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45538784/