自动布局、SnapKit、Layout之约束动画

布局Layout之约束动画

import UIKit

class ViewController: UIViewController {
    
    
    //属性:
    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var widthConstraint: NSLayoutConstraint!
    

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        
        UIView.animateWithDuration(1) {
            
            //改变约束的值的大小
            self.topConstraint.constant = 280
            self.widthConstraint.constant = 250
            
            //如果想要通过改变约束值去动画的改变视图的位置和大小必须添加下面的代码才能有效
            self.view.layoutIfNeeded()
            
        }
        
    }


}

原生方式添加约束

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //手写约束的步骤:
        //1.创建视图对象(不需要设置frame)
        let redView = UIView()
        redView.backgroundColor = UIColor.redColor()
        //2.将视图添加到界面
        self.view.addSubview(redView)
        //3.关闭Autoresizing
        redView.translatesAutoresizingMaskIntoConstraints = false
        
        //4.创建约束对象
        //NSLayoutConstraint:约束类
        //参数1的参数2 参数3 (参数4的参数5)*参数6+参数7
        //a.top
        //redView.Top = (self.view.Top)*1+50
        let top = NSLayoutConstraint.init(item: redView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 50)
        
        //b.left
        let left = NSLayoutConstraint.init(item: redView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 50)
        
        //c.right 
        let right = NSLayoutConstraint.init(item: redView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant:-50)
        
        //d.bottom
        let bottom = NSLayoutConstraint.init(item: redView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant:-200)
        
        //5.添加约束(注意添加约束的对象)
        self.view.addConstraints([top,left,right,bottom])
        
        //再添加一个绿色视图
        //1.创建视图对象
        let greenView = UIView()
        greenView.backgroundColor = UIColor.greenColor()
        //2.添加到界面上
        self.view.addSubview(greenView)
        //3.关闭Autoresizing
        greenView.translatesAutoresizingMaskIntoConstraints = false
        
        //4.创建约束
        //a.left
        let left2 = NSLayoutConstraint.init(item: greenView, attribute: .Left, relatedBy: .Equal, toItem: redView, attribute: .Left, multiplier: 1, constant: 0)
        //b.top
        let top2 = NSLayoutConstraint.init(item: greenView, attribute: .Top, relatedBy: .Equal, toItem: redView, attribute: .Bottom, multiplier: 1, constant: 30)
        //c.width
        let width2 = NSLayoutConstraint.init(item: greenView, attribute: .Width, relatedBy: .Equal, toItem: redView, attribute: .Width, multiplier: 0.5, constant: 0)
        //d.height
        let height2 = NSLayoutConstraint.init(item: greenView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 0, constant: 60)
        
        //5.添加约束
        self.view.addConstraints([left2,top2,width2])
        greenView.addConstraint(height2)
        
        //注意:通过添加的约束必须能够确定视图的坐标和大小

        
        
        
    }

自动布局SnapKit

//将第三方库文件包含进来
import SnapKit
//SnapKit的作用:简便的通过代码添加约束,用法和Masonry一样


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.创建视图对象
        let redView = UIView.init()
        redView.backgroundColor = UIColor.redColor()
        //2.添加到界面上
        self.view.addSubview(redView)
        
        //3.添加约束
        //参数:make,指的就是当前的视图(redView)
        redView.snp_makeConstraints { (make) in
            
            //a.left
            //redView的left 等于 self.view的left偏移20
            make.left.equalTo(self.view.snp_left).offset(20)
            //b.top
            make.top.equalTo(self.view.snp_top).offset(20)
            //c.right
            make.right.equalTo(self.view.snp_right).offset(-20)
            //d.bottom
            make.bottom.equalTo(self.view.snp_bottom).offset(-250)
        }
        
        //再创建一个绿色视图
        //1.创建视图对象
        let greenView = UIView()
        greenView.backgroundColor = UIColor.greenColor()
        //2.添加到界面上
        self.view.addSubview(greenView)
        
        //3.添加约束
        greenView.snp_makeConstraints { (make) in
            //1.left
            //greenView的left等于redView的left(注:如果后边的属性和前面的属性是一样的,后边属性名可以省略)
            make.left.equalTo(redView)
            //2.top
            make.top.equalTo(redView.snp_bottom).offset(20)
            //3.width
            make.width.equalTo(redView).multipliedBy(0.5)
            //4.height
            make.height.equalTo(60)
        }

SnapKit的应用

import UIKit
import SnapKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.创建视图
        let redView = UIView()
        let greenView  = UIView()
        let blueView = UIView()
        
        //2.设置背景颜色
        redView.backgroundColor = UIColor.redColor()
        greenView.backgroundColor = UIColor.greenColor()
        blueView.backgroundColor = UIColor.blueColor()
        
        //3.添加到界面上
        self.view.addSubview(redView)
        self.view.addSubview(greenView)
        self.view.addSubview(blueView)
        
        let margin: CGFloat = 20
        
        //4.添加约束
        //a.绿色视图
        greenView.snp_makeConstraints { (make) in
            make.left.equalTo(self.view).offset(margin)
            make.top.equalTo(self.view).offset(margin)
            make.right.equalTo(redView.snp_left).offset(-margin)
            make.bottom.equalTo(blueView.snp_top).offset(-margin)
            make.height.equalTo(redView)
            make.height.equalTo(blueView)
            make.width.equalTo(redView)
        }
        
        //b.红色视图
        redView.snp_makeConstraints { (make) in
            make.left.equalTo(greenView.snp_right).offset(margin)
            make.top.equalTo(self.view).offset(margin)
            make.right.equalTo(self.view).offset(-margin)
            make.bottom.equalTo(blueView.snp_top).offset(-margin)
            make.height.equalTo(greenView)
            make.height.equalTo(blueView)
            make.width.equalTo(greenView)
        }
        
        
        //c.蓝色视图
        blueView.snp_makeConstraints { (make) in
            make.left.equalTo(greenView)
            make.right.equalTo(redView)
            make.top.equalTo(greenView.snp_bottom).offset(margin)
            make.bottom.equalTo(self.view).offset(-margin)
            make.height.equalTo(greenView)
        }
        
        
        
        
        
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 0、前言 一、Auto Layout前世今生 二、Auto Layout基础知识 1.Auto Layout...
    浮游lb阅读 25,217评论 3 90
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,953评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,380评论 4 61
  • 世上最可悲的事,就是别人坑你,还傻傻的感谢人家。这得有多贱啊!真想给自己一巴掌,知道疼了才长教训。 为什么被忽悠啊...
    白玉山阅读 1,535评论 0 0
  • LAMP是:Linux(操作系统)、Apache(HTTP 服务器),MySQL(有时也指MariaDB,数据库软...
    devindwan阅读 4,379评论 0 0

友情链接更多精彩内容