【问题标题】:Animate adding a subview in a cell with dynamic cell height动画在具有动态单元格高度的单元格中添加子视图
【发布时间】:2017-05-27 16:53:32
【问题描述】:

我想为单元格中子视图的addSubview 设置动画。表格视图单元格具有动态高度,因此折叠和展开动画是原生完成的。

这是代码:

// MARK: UITableViewDelegate

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  if let selectedCell = tableView.cellForRow(at: indexPath) as? MyCell {
    selectedCell.showExtraView()
    expandedCells.insert(indexPath)
  }
  if let prevSelectedCell = tableView.cellForRow(at: prevIndexPath) as? MyCell {
    prevSelectedCell.hideExtraView()
    expandedCells.remove(prevIndexPath)
  }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if expandedCells.contains(indexPath) {
    return CGFloat(100.0)
  } else {
    return CGFloat(80.0)
  }
}

单元格代码:

// MyCell

func showExtraView() {
  contentView.addSubview(extraView)
  contentView.setNeedsLayout()
}

func hideExtraView() {
  extraView.removeFromSuperview()
  contentView.setNeedsLayout()
}

这就是它的样子:

子视图 (extraView) 包括图像和标签。标签动画正确,但图像只是出现在那里,好像它不是动画的一部分。

【问题讨论】:

    标签: ios swift uitableview animation tableviewcell


    【解决方案1】:

    改为动画您视图的 alpha。将动画块内的 alpha 从 0.0 变为 1.0。

    func showExtraView() {
    
        extraView.alpha = 0.0
    
        UIView.animate(withDuration: 0.6) { 
    
            contentView.addSubview(extraView)    
            extraView.alpha = 1.0
            self.contentView.layoutIfNeeded()
    
        }
    }
    
    func hideExtraView() {
    
        UIView.animate(withDuration: 0.6, animations: {
            extraView.alpha = 0.0
            self.contentView.layoutIfNeeded()
        }) { (completed) in
            extraView.removeFromSuperview()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多