首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISwipeGestureRecognizer动画

UISwipeGestureRecognizer动画
EN

Stack Overflow用户
提问于 2017-08-07 02:54:06
回答 2查看 3.9K关注 0票数 5

我试图在我的collectionViewCell中实现一个collectionViewCell,所以当你向左滑动时,这个单元格就消失了。我试图实现的(我找不到方法)是动画的滑动,所以当我在左边的单元格,它消失了一个褪色的效果。这是我在方法cellForItemAtindexPath中的代码

代码语言:javascript
复制
let cSelector = #selector(reset(sender:))
    let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)
    UpSwipe.direction = UISwipeGestureRecognizerDirection.left
    cell.addGestureRecognizer(UpSwipe)

该方法

代码语言:javascript
复制
 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

代码语言:javascript
复制
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(gestureRecognizer:)))
    cell.addGestureRecognizer(gestureRecognizer)

该方法

代码语言:javascript
复制
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)
    }
}

我试图定位这个细胞的坐标,所以当它在细胞左侧的某个点时,细胞会启动某种渐变动画,然后消失。

有什么帮助吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-09 04:54:18

所以我尝试了这个代码,它对我来说很好!

代码语言:javascript
复制
func setupView(){
    // Setting up swipe gesture recognizers
    let swipeUp : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(userDidSwipeUp(_:)))
    swipeUp.direction = .left

    collectionView?.addGestureRecognizer(swipeUp)

    //let swipeDown : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(userDidSwipeDown))
    //swipeDown.direction = .right

    //collectionView?.addGestureRecognizer(swipeDown)
}


func getCellAtPoint(_ point: CGPoint) -> ChatMessageCell? {
    // Function for getting item at point. Note optionals as it could be nil
    let indexPath = collectionView?.indexPathForItem(at: point)
    var cell : ChatMessageCell?

    if indexPath != nil {
        cell = collectionView?.cellForItem(at: indexPath!) as? ChatMessageCell
    } else {
        cell = nil
    }

    return cell
}

func userDidSwipeUp(_ gesture : UISwipeGestureRecognizer) {

    let point = gesture.location(in: collectionView)  //collectionview
    let duration = animationDuration()                //0.5

    if(cell == nil){
        cell = getCellAtPoint(point)

        UIView.animate(withDuration: duration, animations: {
            //self.activeCell.myCellView.transform = CGAffineTransform(translationX: 0, y: -self.activeCell.frame.height)
            self.cell.celdaNormal.transform = CGAffineTransform(translationX: -self.cell.frame.width , y: 0)

        })

    }  else {
        // Getting the cell at the point
        let cell = getCellAtPoint(point)

        // If the cell is the previously swiped cell, or nothing assume its the previously one.
        if cell == nil || cell == cell {
            // To target the cell after that animation I test if the point of the swiping exists inside the now twice as tall cell frame
            let cellFrame = cell?.frame

            var rect = CGRect()

            if cell != nil {
                rect = CGRect(x: (cellFrame?.origin.x)! - (cellFrame?.width)!, y: (cellFrame?.origin.y)!, width: (cellFrame?.width)!*2, height: (cellFrame?.height)!)
            }

            if rect.contains(point) {
                // If swipe point is in the cell delete it

                let indexPath = collectionView?.indexPath(for: cell!)
                messages.remove(at: indexPath!.row)
                collectionView?.deleteItems(at: [indexPath!])

                if messages.count == 0 {
                    reusableView.etiqueta.isHidden = true
                }
            }
            // If another cell is swiped
        }
}

func animationDuration() -> Double {
    return 0.5
}

您所要做的就是调用setupView() in viewDidLoad,仅此而已!我必须提到我从这个问题中修改了代码..。Swipe to delete on CollectionView

票数 1
EN

Stack Overflow用户

发布于 2017-08-07 06:41:27

要实现你的目标,有两个选择。

  1. 创建自定义布局
  2. 使用UIView和滑动手势

创建自定义布局

您可以根据您的动画选择创建自定义布局。Here是参考。你只需要修改它的动画。

使用UIView和滑动手势

遵循以下步骤

  • 将UIView (var名称- swipeView)添加到CollectionView单元中&为UIView设置背景色。
  • 向swipeView中添加滑动手势(左和/或右)
  • 使用不同的滑动手势(开始、拖动、结束)处理视图的滑动和用户的拖动操作。
  • 当滑动手势结束时,用动画将swipeView推到您的单元格外(设置它们的x位置,使其可以超出单元框的界限)。
  • 从数组中删除元素并重新加载集合视图。

我希望,根据上面的逻辑,您可以做您想做的事情,而您可能不需要现成的代码。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45538784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档