【问题标题】:How to update constraints animation in uitableviewcell如何在uitableviewcell中更新约束动画
【发布时间】:2019-08-30 08:03:07
【问题描述】:

我想在UITableView 单元格中使用动画扩展视图高度。 它正在工作,但动画无法按我的意愿工作。 我的代码是这样的。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

         let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell

        //Charactristic expand
        let isExpand = isExpandViewArr[indexPath.row]

        if isExpand == true {

            cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
            UIView.animate(withDuration: 0.5) {
                cell.expandViewHeight.constant = 0
                self.loadViewIfNeeded()
            }
        }else{

            cell.expandBtn.setImage(UIImage(named: "up-arrow"), for: .normal)
            UIView.animate(withDuration: 0.5) {
                cell.expandViewHeight.constant = 40
                self.loadViewIfNeeded()
            }

        }

    }

请查看链接上的屏幕:https://ibb.co/XjjXRz5

【问题讨论】:

  • 你的意思是 它正在工作,但动画没有按我的意愿工作。 ?你期望的行为是什么,你得到了什么?
  • 它正在扩展,但没有动画。我想用动画扩大高度。
  • @nico 打败了我。看看他的回答。

标签: ios swift iphone uitableview uianimation


【解决方案1】:

我相信您需要在动画调用之外设置cell.expandViewHeight.constant = 40,并在内部调用self.layoutIfNeeded()。像这样:

cell.expandViewHeight.constant = 40
UIView.animate(withDuration: 0.5) {            
    self.layoutIfNeeded()
}

【讨论】:

    【解决方案2】:
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
        let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell
    
        //Charactristic expand
        let isExpand = isExpandViewArr[indexPath.row]
    
        if isExpand == true {
    
            cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
            DispatchQueue.main.async {
                self.tblView.beginUpdates()
                cell.expandViewHeight.constant = 0
                self.tblView.endUpdates()
            }
        }else{
    
            cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
            DispatchQueue.main.async {
                self.tblView.beginUpdates()
                cell.expandViewHeight.constant = 40
                self.tblView.endUpdates()
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2020-09-30
      • 1970-01-01
      • 2021-08-26
      相关资源
      最近更新 更多