Swift - UITableView中的渐弱单元格

foo*_*foo 6 fade uitableview tableviewcell swift xcode6

我在Xcode 6上的swift中遇到了我的UITableView的麻烦:

我希望我的UITableView单元在出现/消失时淡入/淡出.

我在网上看了很多关于淡入淡出动画但我找不到我想要的东西,因为它是基于持续时间动画.

我认为我需要的是一种基于alpha的面具?我不是,我甚至不知道要创造一个......

我有一个UIViewController包含一个tableView和一些空白(现在)UITableView的上部和下部.

谢谢你的帮助 !!

最好,

阿纳托利

Joe*_*son 9

我一直在寻找同样的东西,我最终制作了自己的东西,我想我会分享它:

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是细胞:

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
}
Run Code Online (Sandbox Code Playgroud)

结尾看起来像这样:

tableViewCell淡出


Tim*_*mon 7

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
}
Run Code Online (Sandbox Code Playgroud)

编辑:

这会根据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
    }
}
Run Code Online (Sandbox Code Playgroud)