ios - UISwipeGestureRecognizer 动画

标签 ios swift uipangesturerecognizer uiswipegesturerecognizer

我正在尝试在我的 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)
    }
}

我正在尝试定位单元格的坐标,所以当它位于单元格左侧的某个点时,单元格开始播放某种淡入淡出的动画,然后消失。

有什么帮助吗???谢谢!!!

最佳答案

有两种选择可以实现您的目标。

  1. 创建自定义布局
  2. 通过滑动手势使用 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/

相关文章:

php - iOS base64 编码返回与 php 不同的结果

iphone - 使用 coredata 存储/缓存非标准数据类型

ios - 如何在点击后退按钮时显示一次 AdMob Interstitial

swift - 用数据填充 TableView

ios - 设置 UIView 从左上角 (0,0) 开始

ios - 刷新 UITableView 中的分页数据

ios - 根据组织名称或职位获取 iOS 联系人列表

swift - 为什么我在 iOS 上从 Swift 调用服务时会收到 SSL 错误?

ios - (Swift) UIPanGestureRecognizer 在 View 上工作正常,直到将 subview 添加到它

ios - swift 3 : Back gesture