[英]How to animate UIView using constraint from bottom to top?
I am creating UIView programmatically and after that i will animate that view from bottom to top using constraints. 我将以编程方式创建UIView,然后使用约束从下至上为该视图设置动画。 But animations doesnot show on this view object. 但是动画不会显示在该视图对象上。
overlay!.addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor, constant: 20).isActive = true
contentView.trailingAnchor.constraint(equalTo: vc.view.trailingAnchor, constant: -20).isActive = true
contentView.addSubview(messageLabel)
messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8).isActive = true
messageLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8).isActive = true
messageLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
messageLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8).isActive = true
let width: CGFloat = vc.view.frame.width - 40 - 16
stringHeight = calculateEstimatedHeight(width: width, text: text) + 16
contentView.heightAnchor.constraint(equalToConstant: stringHeight).isActive = true
bottomConstraint = contentView.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor, constant: stringHeight)
bottomConstraint.isActive = true
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.showToastViewWithAnimation(viewController: vc)
}
@objc func showToastViewWithAnimation(viewController: Any) {
if let vc = viewController as? ViewController {
vc.view.layoutIfNeeded()
UIView.animate(withDuration: 0.8) {
self.bottomConstraint.constant = -20
vc.view.layoutIfNeeded()
}
}
}
I have pointed out a few things via comments, read the comment and you will understand what is the issue in your code, check below code. 我已经通过注释指出了一些事情,阅读注释,您将了解代码中的问题,请检查以下代码。
=> your code : overlay!.addSubview(contentView)
=>您的代码: overlay!.addSubview(contentView)
@objc func showToastViewWithAnimation(viewController: Any) {
if let vc = viewController as? ViewController {
//vc.view.layoutIfNeeded() - this is not required before updating constraint value
self.bottomConstraint.constant = -20 //update contstraint outside of the animation block
UIView.animate(withDuration: 0.8) {
//vc.view.layoutIfNeeded()
self.overlay?.layoutIfNeeded() //you needs to layout the overlay view, as your other toast views are added in overlay view and not in view of viewcontroller as per your code
}
}
}
Add this line in Your Code: 在您的代码中添加以下行:
self.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
and in Your ShowToastView function first find that which one is bottom constraint and then try to update it like this (Remember its a hint code you can update it according to your requirement): 并在您的ShowToastView函数中首先找到哪个是底部约束,然后尝试像这样进行更新(请记住其提示代码,您可以根据需要对其进行更新):
@objc func showToastViewWithAnimation(viewController: Any) {
if let vc = viewController as? ViewController {
if let bottom = self.contentView.superview?.constraints.first(where: { (constraint) -> Bool in
return constraint.firstAttribute == .bottom}){
// in this condition find your bottom constraint and update it, don't save your constraint in a variable.
bottom.constant = -20
UIView.animate(withDuration: 0.8, animations: {
vc.view.layoutIfNeeded()
}) { (complete) in
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.