【问题标题】:Width constraint change with UIView animation宽度约束随 UIView 动画变化
【发布时间】:2021-02-21 05:11:53
【问题描述】:

我有通过 SnapKit 更新的宽度限制。展开动画:

self.snp.updateConstraints({(make) in
    make.width.equalTo(150.0)
})

折叠动画:

self.snp.updateConstraints({(make) in
    make.width.equalTo(150.0)
})

当我通过以下方式制作动画时:

UIView.animate(withDuration: 0.5,animations: {
    self.layoutIfNeeded()
}, completion: nil)

我的视图通过首先向左扩展,然后从左向右扩展来改变宽度,因为我的“动画”视图尾随锚点等于超级视图尾随锚点。

slider.snp.makeConstraints({(make) in
    make.trailing.equalToSuperview()
    make.centerY.equalToSuperview()
    make.height.equalTo(slider.getContentHeight())
    make.width.equalTo(slider.labels.first?.intrinsicContentSize.width ?? 30.0)
})

所以我希望动画从右向左扩展

【问题讨论】:

    标签: ios swift animation constants snapkit


    【解决方案1】:

    我通过更改 view.center.x 达到了预期的效果。我试图改变前导约束和宽度,但没有成功。我也碰巧改变了视图的宽度,但它在没有动画的情况下发生,并且分别在展开/折叠之前/之后发生。所以这是我的代码:

    self.snp.updateConstraints({(make) in
        make.width.equalTo(150.0)
    })
    
    UIView.animate(withDuration: 0.2,
        animations: {
            self.center.x -= 90
            self.layoutIfNeeded()
        }, completion: nil)
    

    反过来:

    UIView.animate(withDuration: 0.2,
        animations: {
            self.center.x += 90
            self.layoutIfNeeded()
        }, completion: nil)
    
    self.snp.updateConstraints({(make) in
        make.width.equalTo(30.0)
    })
    

    动画在折叠时有点粗糙,但在展开时效果很好。仍然感谢任何建议。

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2017-08-23
      • 2017-02-26
      相关资源
      最近更新 更多