2

私はcollectionViewCellでUISwipeGestureRecognizerを実装しようとしていますので、左にスワイプするとセルは消えます。私は実装しようとしている(私はそれを行う方法を見つけることができません)スワイプをアニメーションするので、私は左にセルをスワイプすると、フェード効果で消えます。これは私がメソッド内cellForItemAtindexPathUISwipeGestureRecognizer animation

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

あなたの目標を達成するための2つのオプションがあります。

  1. スワイプジェスチャー


でカスタムレイアウト

  • 使用するUIViewを作成してカスタムレイアウト
    を作成するには、アニメーションのお好みに応じてカスタムレイアウトを作成することができます。 Hereは参考になります。アニメーションを変更するだけです。


    スワイプジェスチャーで使用するUIView
    は、次の手順に従い

    • のUIView(VAR名 - swipeView)を追加CollectionViewセルにUIViewのための&セットの背景色を。
    • スワイプジェスチャー(左、右)をスワイプビューに追加
    • スワイプジェスチャーのさまざまな状態(開始、終了、終了)を使用して、ユーザーのドラッグ操作と一緒にスクロールを処理します。
    • ときスワイプジェスチャー終了、配列から要素を削除し、コレクションビューをリロードアニメーション(それは、セルフレームの境界の外に行くことができるようにそれらのセットのx位置)
    • であなたのセルの側からswipeViewを押してください。

    上記のロジックで、あなたが望むことができ、あなたはレディメイドコードを必要としないことを望みます。

  • +0

    ヘルプマンに感謝! – Edwjonn