2015-04-02 26 views
3

衰落細胞我有在Xcode中6與我迅速的UITableView的煩惱:斯威夫特 - 在UITableView的

我想我的UITableView細胞淡入/淡出出現/消失時。

我在網上看了很多淡入淡出的動畫,但是我沒有找到我想要的,因爲它是基於持續時間動畫的。

我想我需要的是基於阿爾法的面具嗎?我不是sur,我甚至不知道必須創建一個...

我有一個UIViewController,包含一個tableView和一些空白空間(現在)UITableView的上下。

感謝您的幫助!

最佳,

阿納託利

+0

從未嘗試過這一點,但嘗試設置不透明度爲0,然後再使用'的tableView( _:willDisplayCell:forRowAtIndexPath:)'將動畫設置爲1. – Rengers 2015-04-02 09:35:20

回答

4

animateWithDuration似乎是恰當的,因爲進/出具有持續時間淡出:細胞逐漸隨着時間推移變得可見或不可見。

這裏是製作表格單元格的一個例子淡入:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

    cell.textLabel?.text = self.items[indexPath.row] 
    cell.backgroundColor = UIColor.grayColor() 
    cell.alpha = 0 

    UIView.animateWithDuration(2, animations: { cell.alpha = 1 }) 

    return cell 
} 

編輯:

這都將根據自己的y位置,這可能是更接近細胞的Alpha你想要什麼:

func scrollViewDidScroll(scrollView: UIScrollView) { 

    for cell in tableView.visibleCells() as [UITableViewCell] { 

     var point = tableView.convertPoint(cell.center, toView: tableView.superview) 
     cell.alpha = ((point.y * 100)/tableView.bounds.maxY)/100 
    } 
} 
+0

您好Timon,非常感謝您的幫助! 就像我說過的,我不是在尋找一個持續時間的動畫,因爲當用戶滾動速度非常快時,所有的屏幕都會消失,而當他滾動非常慢時,動畫不夠長。 如果存在,我需要一個特定距離的動畫。另外,我想如果有任何Mask方法存在。通過在tableView的頂部和底部應用alpha梯度蒙版,它將允許淡入淡出細胞。 對不起,我知道我問了很多,但我沒有在網上找到這樣的東西... 再次感謝,希望你有一個想法! Best, Anatole – foofoo 2015-04-03 19:25:57

+0

我在上面添加了另一個解決方案,根據單元格的y位置更改alpha。如果將此擴展爲僅應用於屏幕的前三分之一,並將屏幕底部三分之一的效果顛倒過來,那應該會讓您非常靠近? – Timon 2015-04-05 12:22:12

+0

非常感謝timon這是完美的!我會盡快測試,我有一些空閒時間! – foofoo 2015-04-06 16:54:42

5

我一直在尋找同樣的事情,我最終使我自己的,我想我會分享它:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 

    let cellCount = tableView.visibleCells.count 
    let alphaInterval:CGFloat = 1.0/CGFloat(cellCount/2) 

    for (index,cell) in (tableView.visibleCells as [UITableViewCell]).enumerated() { 

     if index < cellCount/2 { 
      cell.alpha = CGFloat(index) * alphaInterval 
     } else { 
      cell.alpha = CGFloat(cellCount - index) * (alphaInterval) 
     } 
    } 
} 

,這裏是小區:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    cell.textLabel?.text = countries[indexPath.row].uppercased() 
    cell.textLabel?.textColor = .white 
    cell.backgroundColor = .clear 
    cell.alpha = 0.25 

    UIView.animate(withDuration: 0.5, animations: { 
     cell.alpha = 1 
    }) 

    return cell 
} 

最終看起來像這樣:

tableViewCell fade