2

我試圖在我的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://stackoverflow.com/questions/16690831/uicollectionview-animations-insert-delete-items – Krunal

+0

事情是,我的單元格寬度是collectionView的整個寬度,所以我正在尋找一個滑動左旋效應;像UITableView,但與我有的代碼。當然快! – Edwjonn

回答

0

所以我試過這段代碼,它對我來說工作正常!

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

有兩個選項可以實現您的目標。

  1. 創建自定義佈局
  2. 使用的UIView與輕掃手勢


創建自定義佈局
您可以根據您所選擇的動畫創建自定義佈局。 Here是參考。你只需要修改它的動畫。


使用的UIView與輕掃手勢
按照以下步驟

  • 添加的UIView(VAR的名字 - swipeView)中的CollectionView細胞&設置背景顏色的UIView。
  • 將滑動手勢(左側和/或右側)添加到swipeView中
  • 使用滑動手勢(開始,拖動,結束)的不同狀態處理滑動視圖以及用戶的拖動操作。
  • 當滑動手勢結束時,從陣列推swipeView出側的細胞與
  • 刪除元素的動畫(其中組x的位置,使得它可以出去細胞幀的邊界),並且重新加載集合視圖。

我希望,通過上述邏輯,你可以做你想做的事情,而且你可能不需要現成的代碼。

+0

謝謝你的幫助! – Edwjonn