UISwipeGestureRecognizer动画

问题描述:

我试图在我的collectionViewCell中实现UISwipeGestureRecognizer,因此当您向左滑动时,单元格消失。我试图实现(我找不到方法)是为了刷动画,所以当我向左滑动单元格时,它会消失并带有淡化效果。这是代码我有方法cellForItemAtindexPathUISwipeGestureRecognizer动画

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

我试图找到该单元格的坐标,所以当它在一个点的方法细胞左侧,细胞星星会出现某种淡入淡出的动画,然后消失。

任何帮助?谢谢!!!

+0

是的,我想删除它与动画。 – Edwjonn

+0

这可能会帮助你https://*.com/questions/16690831/uicollectionview-animations-insert-delete-items – Krunal

+0

事情是,我的单元格宽度是collectionView的整个宽度,所以我正在寻找一个滑动左旋效应;像UITableView,但与我有的代码。当然快! – Edwjonn

所以我试过这段代码,它对我来说工作正常!

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 
} 

所有你所要做的,就是调用viewDidLoad中的setupView(),这就是它! 我不得不提,我修改了这个问题的代码... Swipe to delete on CollectionView

有两个选项可以实现您的目标。

  1. 创建自定义布局
  2. 使用的UIView与轻扫手势


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


使用的UIView与轻扫手势
按照以下步骤

  • 添加的UIView(VAR的名字 - swipeView)中的CollectionView细胞&设置背景颜色的UIView。
  • 将滑动手势(左侧和/或右侧)添加到swipeView中
  • 使用滑动手势(开始,拖动,结束)的不同状态处理滑动视图以及用户的拖动操作。
  • 当滑动手势结束时,从阵列推swipeView出侧的细胞与
  • 删除元素的动画(其中组x的位置,使得它可以出去细胞帧的边界),并且重新加载集合视图。

我希望,通过上述逻辑,你可以做你想做的事情,而且你可能不需要现成的代码。

+0

谢谢你的帮助! – Edwjonn