简体   繁体   English

UISwipeGestureRecognizer动画

[英]UISwipeGestureRecognizer animation

I'm trying to implement an UISwipeGestureRecognizer in my collectionViewCell, so when you swipe to the left, the cell disappear. 我正在尝试在我的collectionViewCell中实现UISwipeGestureRecognizer,所以当你向左滑动时,单元格会消失。 What i'm trying to implement (i can't find a way to do it) is to animate the swipe, so when i swipe the cell to the left, it disappears with a fade effect. 我正在尝试实现的目标(我无法找到一种方法)是为滑动设置动画,所以当我向左滑动单元格时,它会以淡入淡出效果消失。 This is the code i have inside the method cellForItemAtindexPath 这是我在方法cellForItemAtindexPath的代码

let cSelector = #selector(reset(sender:))
    let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)
    UpSwipe.direction = UISwipeGestureRecognizerDirection.left
    cell.addGestureRecognizer(UpSwipe)

The method 方法

 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()

}

Thanks!!! 谢谢!!!

EDIT: I think i found an easiest way to do it, but i'm having some troubles. 编辑:我想我发现了一种最简单的方法,但我遇到了一些麻烦。 I tried implementing a UIPanGestureRecognizer in the cell. 我尝试在单元格中实现UIPanGestureRecognizer。 This is how it looks like... 这是它的样子......

cellForItemAt cellForItemAt

let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(gestureRecognizer:)))
    cell.addGestureRecognizer(gestureRecognizer)

The method 方法

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)
    }
}

I'm trying to locate the coordinates of the cell, so when it's in a point at the left of the cell, the cell stars some kind of fade animation, and then disappear. 我正在尝试找到单元格的坐标,所以当它位于单元格左侧的某个点时,单元格会出现某种淡入淡出动画,然后消失。

Any help??? 任何帮助??? Thanks!!! 谢谢!!!

There are two options to achieve your goal. 有两种方法可以实现您的目标。

  1. Create custom layout 创建自定义布局
  2. Use UIView with swipe gesture 使用UIView滑动手势


Create custom layout 创建自定义布局
You can create a custom layout according to your choice of animation. 您可以根据您选择的动画创建自定义布局。 Here is reference. 是参考。 You just need to modify its animation. 你只需要修改它的动画。


Use UIView with Swipe Gesture 使用UIView和Swipe Gesture
Follow these steps 跟着这些步骤

  • Add UIView (var name - swipeView ) in CollectionView Cell & set background color for UIView. 在CollectionView Cell中添加UIView(var name - swipeView )并为UIView设置背景颜色。
  • Add Swipe Gesture (left and/or right)to swipeView 向swipeView添加滑动手势(向左和/或向右)
  • Handle swipe of view along with user's drag operation using different states of swipe gesture (begin, drag, end). 使用不同的滑动手势状态(开始,拖动,结束)处理滑动视图以及用户的拖动操作。
  • When swipe gesture end, push swipeView out side your cell with animation (set x position of them such that it can go out of bounds of cell frame) 当滑动手势结束时,使用动画将swipeView推出您的单元格(设置它们的x位置,使其超出单元格范围)
  • Remove element from array and reload collection view. 从数组中删除元素并重新加载集合视图。

I hope, with above logic you can do what you want and you may not need readymade code. 我希望,凭借上述逻辑,你可以做你想做的事,你可能不需要现成的代码。

So i tried this code, and it works fine for me! 所以我尝试了这个代码,它对我来说很好!

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
}

All you have to do, is call the setupView() in viewDidLoad, and that's it! 你所要做的就是在viewDidLoad中调用setupView() ,就是这样! I have to mention that i modified the code from this question... Swipe to delete on CollectionView 我必须提到我修改了这个问题的代码... 在CollectionView上滑动删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM