动画约束,如何在所需方向上增长高度

问题描述 投票:1回答:1

我使用SnapKit根据触摸事件改变UIView的高度。因此,当用户触摸单元格时,它会扩展。这样工作正常:

@objc func didRecieveTouch() {
    let originalHeight: CGFloat = 90
    let expandedHeight: CGFloat = 244

    if !expanded {
        self.snp.updateConstraints { make in
            make.height.equalTo(expandedHeight)
        }
    } else {
        self.snp.updateConstraints { make in
            make.height.equalTo(originalHeight)
        }

    }
    self.expanded = !self.expanded
    UIView.animate(withDuration: 0.25, animations: {
        self.superview?.layoutIfNeeded()
    })
}

问题是视图增长的方向对我来说看起来很奇怪和错误。该视图实际上“跳”到新分配的空间并向上增长而不是向下增长,这是我想要的期望行为。

enter image description here

swift constraints snapkit
1个回答
-2
投票

在动画块中添加代码。

UIView.animate(withDuration: 0.25) {
    if !expanded {
        self.snp.updateConstraints { make in
            make.height.equalTo(expandedHeight)
        }
    } else {
        self.snp.updateConstraints { make in
            make.height.equalTo(originalHeight)
       }
    }
    self.expanded = !self.expanded
    self.superview?.layoutIfNeeded()
 }
© www.soinside.com 2019 - 2024. All rights reserved.