簡體   English   中英

UISwipeGestureRecognizer動畫

[英]UISwipeGestureRecognizer animation

我正在嘗試在我的collectionViewCell中實現UISwipeGestureRecognizer,所以當你向左滑動時,單元格會消失。 我正在嘗試實現的目標(我無法找到一種方法)是為滑動設置動畫,所以當我向左滑動單元格時,它會以淡入淡出效果消失。 這是我在方法cellForItemAtindexPath的代碼

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

我正在嘗試找到單元格的坐標,所以當它位於單元格左側的某個點時,單元格會出現某種淡入淡出動畫,然后消失。

任何幫助??? 謝謝!!!

有兩種方法可以實現您的目標。

  1. 創建自定義布局
  2. 使用UIView滑動手勢


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


使用UIView和Swipe Gesture
跟着這些步驟

  • 在CollectionView Cell中添加UIView(var name - swipeView )並為UIView設置背景顏色。
  • 向swipeView添加滑動手勢(向左和/或向右)
  • 使用不同的滑動手勢狀態(開始,拖動,結束)處理滑動視圖以及用戶的拖動操作。
  • 當滑動手勢結束時,使用動畫將swipeView推出您的單元格(設置它們的x位置,使其超出單元格范圍)
  • 從數組中刪除元素並重新加載集合視圖。

我希望,憑借上述邏輯,你可以做你想做的事,你可能不需要現成的代碼。

所以我嘗試了這個代碼,它對我來說很好!

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() ,就是這樣! 我必須提到我修改了這個問題的代碼... 在CollectionView上滑動刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM