我使用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()
})
}
问题是视图增长的方向对我来说看起来很奇怪和错误。该视图实际上“跳”到新分配的空间并向上增长而不是向下增长,这是我想要的期望行为。
在动画块中添加代码。
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()
}