【问题标题】:UITableView cell height animationUITableView 单元格高度动画
【发布时间】:2016-07-05 15:37:30
【问题描述】:

我做小部件。我需要用动画改变单元格的高度。当点击“显示更多”去额外的控制。 enter image description here

我写了这段代码

        tableView.beginUpdates()
        showModes(alpha: 1, duration: 0.2, delay: 0.1)
        tableView.endUpdates()

func showModes(alpha: CGFloat, duration: CGFloat, delay: CGFloat) {
    if tableView.numberOfRows(inSection: 0) != 0 {
        for i in 0...tableView.numberOfRows(inSection: 0)-1 {
            let indexPath = IndexPath(row: i, section: 0)
            if let cell = tableView.cellForRow(at: indexPath) as? WidgetCell {
                UIView.animate(withDuration: TimeInterval(duration), delay: TimeInterval(delay) ,animations: {
                    if alpha == 0 {
                        cell.favoriteConstraint.constant = -45
                    } else {
                        cell.favoriteConstraint.constant = 10
                    }
                    cell.favoriteMode.alpha = alpha
                    self.tableView.layoutIfNeeded()
                })

            }
        }
    }
}

但它并不顺利。它剧烈地上下抽搐。如何让它顺利运行,例如,在 Apple 的天气小部件中?

【问题讨论】:

    标签: xcode swift uitableview animation today-extension


    【解决方案1】:

    我无法明确识别出什么是问题,因为您只发布了部分代码。但我可以建议做几项检查

    1. 您是否为 UITableView 设置了自调整单元格? AtableView.rowHeight = UITableViewAutomaticDimension;
    2. 您是否在单元格的 contentView 中添加了约束(favoriteConstraint)

    此外,当动画约束更改时,您不需要在动画闭包内更改它。

    标准代码的样子

    cell.favoriteConstraint.constant = newValue
        UIView.animateWithDuration(0.3) {
        cell.contentView.layoutIfNeeded()
    }
    

    你也不需要包装你的方法调用。这一定没问题:

    showModes(alpha: 1, duration: 0.2, delay: 0.1)
    tableView.beginUpdates()    
    tableView.endUpdates()
    

    【讨论】:

    • 是的,都添加了 override func viewDidLoad() { super.viewDidLoad() ledControllers = readLED() self.tableView.estimatedRowHeight = 300 self.tableView.rowHeight = UITableViewAutomaticDimension } 我录制了一个视频来制作它更清楚。第一个单元格展开,然后动画才开始。我希望它更像一个天气小部件youtu.be/f4D7NnoyWrQ P.S. Судя по нику вы говорите по русски?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    相关资源
    最近更新 更多