我想设置视图如何从屏幕底部滑动。但我不能这样做,而是从中心获得改变几何的动画。它看起来像这样
https://i.stack.imgur.com/aIYwC.gif
以下是
的观点class Notes: UIView {
var topConstraint: NSLayoutConstraint?
lazy var overlay: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .black
view.alpha = 0.7
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return view
}()
lazy var contentView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.alpha = 1
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
return view
}()
override init(frame: CGRect) {
super.init(frame: UIScreen.main.bounds)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
addSubview(overlay)
addSubview(contentView)
contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 16).isActive = true
contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true
contentView.heightAnchor.constraint(equalToConstant: 400).isActive = true
topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800)
topConstraint?.isActive = true
}
func animate() {
self.topConstraint?.constant = 80
UIView.animate(withDuration: 0.3, animations: {
self.contentView.layoutIfNeeded()
})
}
}
这就是我开球的方式
guard let window = UIApplication.shared.keyWindow else { return }
let notes = Notes()
window.addSubview(notes)
notes.animate()
我做错了什么?
答案 0 :(得分:0)
您没有以正确的方式激活约束,也无法再次调用错误的布局视图
class Notes: UIView {
var topConstraint: NSLayoutConstraint?
lazy var overlay: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .black
view.alpha = 0.7
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return view
}()
lazy var contentView: UIView = {
let view = UIView()
view.backgroundColor = .green
view.alpha = 1
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
return view
}()
override init(frame: CGRect) {
super.init(frame: UIScreen.main.bounds)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
addSubview(overlay)
addSubview(contentView)
topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800)
// you can activate your constraints directly like this
// I changed left and right to leading and trailing but that's a matter of choice
// remember if your supporting left to right languages in your app then not use left and right better use Leading and Trailing
NSLayoutConstraint.activate([
contentView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
contentView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -16),
contentView.heightAnchor.constraint(equalToConstant: 400),
topConstraint!
])
}
func animate() {
self.topConstraint?.constant = 80
UIView.animate(withDuration: 0.3, animations: {
// here you should call self.layoutIfNeeded so self which is a UIView should layout again
self.layoutIfNeeded()
})
}
}
答案 1 :(得分:0)
self.layoutIfNeeded()
功能结束时致电setupViews()
。self.layoutIfNeeded()
中,而不是内容视图。 休息一切都很好。
class Notes: UIView {
var topConstraint: NSLayoutConstraint?
lazy var overlay: UIView = {
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .black
view.alpha = 0.7
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return view
}()
lazy var contentView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.alpha = 1
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
return view
}()
override init(frame: CGRect) {
super.init(frame: UIScreen.main.bounds)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
addSubview(overlay)
addSubview(contentView)
contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 16).isActive = true
contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true
contentView.heightAnchor.constraint(equalToConstant: 400).isActive = true
topConstraint = contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 800)
topConstraint?.isActive = true
self.layoutIfNeeded()
}
func animate() {
self.topConstraint?.constant = 80
UIView.animate(withDuration: 0.3, animations: {
self.layoutIfNeeded()
})
}
}