簡體   English   中英

加載時以Swift方式對TableView單元進行動畫處理(向上進入TableView)

[英]Animate TableView Cells in Swift (Up into TableView) on Loading

我已經設置了此代碼,以便在顯示時調用並為tableview單元設置動畫。 我該如何設置它,使其僅對視圖的初始加載中出現的單元格進行動畫處理?

提前致謝...

  var preventAnimation = Set<NSIndexPath>()

  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // 
    if !preventAnimation.contains(indexPath) {
        preventAnimation.insert(indexPath)
        let rotationTransform = CATransform3DTranslate(CATransform3DIdentity, 0, +600, 0)
        cell.layer.transform = rotationTransform
        UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .CurveEaseOut, animations: {
            cell.layer.transform = CATransform3DIdentity
            }, completion: nil)
    }
}

您可以僅使用變量maxCellIndexAppeared來重新編碼已出現單元格的最大indexPath.row ,這比使用包含所有indexPath的集合更好。

在初始化方法中,將maxCellIndexAppeared設置為零,然后在顯示單元格時將其與indexPath.row進行比較。如果當前單元格的indexPath大於變量,則返回動畫,否則返回。

像這樣:

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if maxCellIndexAppeared > indexPath.row {
           return
        }

        //here reset the current maximum index
        maxCellIndexAppeared = indexPath.row
        //And then do your animation
       ....do your animation.....
   }
//Declare Bool array which will keep track of the rows which are animated 
var animationShown : [Bool]? 


// Initalize animationShown array in your viewDidLoad 
self.animationShown = [Bool] (repeating: false, count: YourTableViewRowsCount)


// MARK: - Animate Tableview Cell
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if self.animationShown?[indexPath.row] == false
{
//Transform Animation
let transform = CATransform3DTranslate(CATransform3DIdentity, -200, 0, 0)
cell.layer.transform = transform
UIView.animate(withDuration: 0.5, animations:
{
cell.layer.transform = CATransform3DIdentity
})
self.animationShown?[indexPath.row] = true
}
}

Hope this helps!! Happy Coding...

暫無
暫無

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

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