我想问一下如何使用自动布局对UIView高度进行动画处理。我点击时的第一个视图会扩展,但是第二个和第三个视图则不会扩展。即使我尝试打印它,仍然不打印这是我的代码。
这是我在xib中的设置,将middleContainer和bottomContainer优先级设置为低。并为middleContainer和bottomContainer设置topConstraint常量25
@IBOutlet weak var topContainerHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var middleContainerHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomContainerHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var tableViewMiddleConstraint: NSLayoutConstraint!
@IBOutlet weak var tableViewBottomConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
calendarView.alpha = 0
let topTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTopTap))
let middleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleMiddleTap))
let bottomTapGesture = UITapGestureRecognizer(target: self, action: #selector(handlebottomTap))
topContainer.addGestureRecognizer(topTapGesture)
middleContainer.addGestureRecognizer(middleTapGesture)
bottomContainer.addGestureRecognizer(bottomTapGesture)
}
@objc func handleTopTap(gesture: UITapGestureRecognizer) {
// print("Tapped")
if topContainerHeightConstraint.constant == 75 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.topContainerHeightConstraint.constant = 350
self.calendarView.alpha = 1
})
} else {
defaultConstraint()
}
}
@objc func handleMiddleTap(gesture: UITapGestureRecognizer) {
print("Tapped")
if middleContainerHeightConstraint.constant == 75 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.middleContainerHeightConstraint.constant = 110
self.tableViewMiddleConstraint.constant = 110
})
} else {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.middleContainerHeightConstraint.constant = 75
self.tableViewMiddleConstraint.constant = 0
})
}
}
@objc func handlebottomTap(gesture: UITapGestureRecognizer) {
if bottomContainerHeightConstraint.constant == 75 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.bottomContainerHeightConstraint.constant = 110
self.tableViewBottomConstraint.constant = 162
})
} else {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
self.bottomContainerHeightConstraint.constant = 75
self.tableViewBottomConstraint.constant = 0
})
}
}
fileprivate func defaultConstraint() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.arrowImageView.image = #imageLiteral(resourceName: "down-chevron")
self.topContainerHeightConstraint.constant = 75
self.calendarView.alpha = 0
self.calendarView.layoutIfNeeded()
})
}
您能帮我在哪里做错什么?
动画流程应该像
self.topContainerHeightConstraint.constant = 75 // 1 change constant
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.layoutIfNeeded() // layout superView
})