簡體   English   中英

在TableViewCell中快速動畫視圖

[英]Swift animate view in tableviewcell

對於正在開發的應用程序,我使用的是表格視圖。 我的tableview,當然有一個tableviewcell。 在該tableviewcell中,我以編程方式添加了一個視圖(因此在情節提要中沒有任何內容)。 在此視圖中,我繪制了一些線條和文本,這變成了消息泡。 如果您也向其他用戶發送了消息氣泡,則氣泡行將打開。

所以我在該UIView的類中有動畫功能(sendbubble.swift)

現在,它已經檢查是否被讀取,並打開正確的氣泡。 但是通常它應該在0.6秒內動畫(打開的線應該旋轉)。 但是它會立即動畫。 所以我的問題是,如何在持續時間內為其設置動畫?

我還希望仍在我的自定義UIView類(sendbubble.swift)中調用它。 也許我需要函數中的代碼來檢查手機上是否顯示了該單元格?

提前致謝!

func openMessage() {
    UIView.animate(withDuration: 0.6, delay: 0.0, options: [], animations: {
        var t = CATransform3DIdentity;
        t = CATransform3DMakeRotation(CGFloat(3 * Float.pi / 4), 0, 0, 1)
        self.moveableLineLayer.transform = t;
    }, completion:{(finished:Bool) in })
}

您需要實現UITableViewDelegate方法tableView(_:willDisplay:forRowAt:) https://developer.apple.com/reference/uikit/uitableviewdelegate/1614883-tableview

這是在要繪制單元格時觸發的,而不是在創建單元格時觸發的。 您還需要在模型中存儲一個狀態,該狀態說明是否發生了動畫,否則它將在每次單元格返回視圖時發生。

在視圖控制器中(偽代碼)

class CustomViewController: UITableViewDelegate {
    //where ever you define your tableview
    var tableView:UITableView
    tableView.delegate = self

    var dataSource //some array that is defining your cells - each object has a property call hasAnimated

    func tableView(_ tableView: UITableView, willDisplay cell: ITableViewCell, 
           forRowAt indexPath: IndexPath) {
        if let cell = cell as? CustomTableViewCell, dataSource[indexPath.row].hasAnimated == false {
            dataSource[indexPath.row].hasAnimated = true
            cell.openMessage()
        }
    }
}

class CustomTableViewCell: UITableViewCell {
    func openMessage() { //your method
    }
} 

首先,您需要抓住單元格。

  1. 獲取需要顯示氣泡的那個單元格的indexPath。
  2. tableView.cellForRowAt(at:indexPath)獲取單元格
  3. 現在我們可以訪問該氣泡視圖,現在您可以使用相同的函數func openMessage()動畫處理

任何問題? 評論。

暫無
暫無

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

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