美文网首页
iOS autolayout animation , snp

iOS autolayout animation , snp

作者: 老猫_2017 | 来源:发表于2020-04-25 16:56 被阅读0次

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)
        }
  1. UILayoutGuide 虚拟占位操作
  2. NSLayoutAnchor 来简化 NSLayoutConstraint 的创建
  3. UILayoutSupport 是协议, 仅被UIViewController属性topLayoutGuide和bottomLayoutGuide实现,用于定义视图内容与栏位(UIKit Bar)的显示层级关系
  4. NSLayoutAnchor - NSLayoutConstraint - UILayoutGuide - UILayoutSupport

总结 autolayout 下,动画的几种方式

  1. invalidateIntrinsicContentSize, 子类重写 IntrinsicContentSize 如第一个方式来实现动画效果
  2. 调整 layout 的 constraint 来实现动画效果,
  3. 通过frame 来实现动画效果,精细化控制

相关文章

网友评论

      本文标题:iOS autolayout animation , snp

      本文链接:https://www.haomeiwen.com/subject/riukwhtx.html