2015-04-02 114 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