在第二个动画之前查看跳跃

View jumps before second animation

我正在尝试为 table 视图设置动画,使其看起来像 drop-down 菜单,视图显示正常(尽管我更喜欢从上到下显示,目前是从中间向外显示)问题是当我点击按钮再次关闭它时,table 视图跳转然后消失,然后按下按钮之后什么都不做。第二次点击按钮后出现约束错误。

这是点击按钮时的调用方式:

    let height: CGFloat = tableViewOpen ? 0 : 300

    UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: {
        self.tableView.set(height: height)
        self.tableView.layoutIfNeeded()
    }).startAnimation()

    tableViewOpen = !tableViewOpen

我的 tableView 约束是使用我工作的公司的一些扩展设置的,但看起来像这样:

 tableView.pin(.top, to: .bottom, of: breedButton)
 tableView.pin(toSuperview: [.leading, .trailing])
 tableView.set(width: 250)

更多详细信息 - table 视图在 child viewcontroller 中,它只是品种按钮和 table 视图。 child viewcontroller的约束设置如下:

    filterViewController.view.pin(.top, to: .top, of: titleLabel)
    filterViewController.view.pin(toSuperview: .leading)
    filterViewController.view.set(width: 250)

没有为 table 视图或 childVC 设置高度,不确定这是否是问题所在。

点击第二个按钮后出现约束错误:

(
    "<NSLayoutConstraint:0x600003e53a20 UITableView:0x7fe090829000.height == 300   (active)>",
    "<NSLayoutConstraint:0x600003e2c050 UITableView:0x7fe090829000.height == 0   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003e53a20 UITableView:0x7fe090829000.height == 300   (active)>

问题很明显,您在单击此处的每个按钮时都设置了高度限制

 self.tableView.set(height: height)

但是你应该用

创建一次高度限制
var tableHeight:NSLayoutConstraint!

并将其设置为 viewDidLoad ,然后将其更改为常量

tableHeight.constant = show ? 300 : 0 
 UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: { 
     self.view.layoutIfNeeded()
 }).startAnimation()