animate-intrinsiccontentsize-changes
... 进行布局
[UIView animateWithDuration:0.2 animations:^{
[self invalidateIntrinsicContentSize];
[self.superview layoutIfNeed];
}];
UIView.animate(withDuration: 0.2, animations: {
self.invalidateIntrinsicContentSize()
self.superview?.setNeedsLayout()
self.superview?.layoutIfNeeded()
})
self.topView.snp.updateConstraints { (make) in
make.top.equalToSuperview().offset(-50)
}
self.topView.needsUpdateConstraints()
UIView.animate(withDuration: 0.2) {
self.view.layoutIfNeeded()
}
// Compression Resistance
View.height >= 0.0 * NotAnAttribute + IntrinsicHeight
View.width >= 0.0 * NotAnAttribute + IntrinsicWidth
// Content Hugging
View.height <= 0.0 * NotAnAttribute + IntrinsicHeight
View.width <= 0.0 * NotAnAttribute + IntrinsicWidth
let layoutGuide1 = UILayoutGuide()
view.addLayoutGuide(layoutGuide1)
let layoutGuide2 = UILayoutGuide()
view.addLayoutGuide(layoutGuide2)
let redBtn = UIButton()
redBtn.backgroundColor = .red
view.addSubview(redBtn)
let blueBtn = UIButton()
blueBtn.backgroundColor = .blue
view.addSubview(blueBtn)
layoutGuide1.snp.makeConstraints { (make) in
make.top.equalTo(100)
make.leading.equalTo(10)
make.width.equalTo(80)
make.height.equalTo(100)
}
redBtn.snp.makeConstraints { (make) in
make.top.equalTo(layoutGuide1.snp.top)
make.leading.equalTo(layoutGuide1.snp.trailing).offset(10)
make.width.equalTo(layoutGuide1.snp.width)
make.height.equalTo(layoutGuide1.snp.height)
}
layoutGuide2.snp.makeConstraints { (make) in
make.top.equalTo(layoutGuide1.snp.top)
make.leading.equalTo(redBtn.snp.trailing).offset(10)
make.width.equalTo(layoutGuide1.snp.width)
make.height.equalTo(layoutGuide1.snp.height)
}
blueBtn.snp.makeConstraints { (make) in
make.top.equalTo(layoutGuide1.snp.top)
make.leading.equalTo(layoutGuide2.snp.trailing).offset(10)
make.width.equalTo(layoutGuide1.snp.width)
make.height.equalTo(layoutGuide1.snp.height)
}
- UILayoutGuide 虚拟占位操作
- NSLayoutAnchor 来简化 NSLayoutConstraint 的创建
- UILayoutSupport 是协议, 仅被UIViewController属性topLayoutGuide和bottomLayoutGuide实现,用于定义视图内容与栏位(UIKit Bar)的显示层级关系
- NSLayoutAnchor - NSLayoutConstraint - UILayoutGuide - UILayoutSupport
总结 autolayout 下,动画的几种方式
- invalidateIntrinsicContentSize, 子类重写 IntrinsicContentSize 如第一个方式来实现动画效果
- 调整 layout 的 constraint 来实现动画效果,
- 通过frame 来实现动画效果,精细化控制
网友评论