[xamarin]山寨Masonry封装AutoLayout

var v1 = new UIView ();
v1.BackgroundColor = UIColor.Green;
this.View.AddSubview (v1);

//v1距父视图左边100,右边10,头部10,高为100
v1.MakeConstraints (this.View, ns => {
    ns.Left.EqualTo(this.View, XYLCAttrEnum.Left).Offset(100);
    ns.Right.EqualTo(this.View, XYLCAttrEnum.Right).Offset(-10);
    ns.Top.EqualTo(this.View, XYLCAttrEnum.Top).Offset(10);
    ns.Height.SetValue(100);
});
    
var v2 = new UIView ();
v2.BackgroundColor = UIColor.Red;
this.View.AddSubview (v2);

//v2长宽50,中心位于v1的中心
v2.MakeConstraints (this.View, ns => {
    ns.Width.SetValue(50);
    ns.Height.SetValue(50);
    ns.CenterX.EqualTo(v1);
    ns.CenterY.EqualTo(v1);
});
[xamarin]山寨Masonry封装AutoLayout_第1张图片
1.png

源码

using System;
using System.Collections.Generic;
using UIKit;
using Foundation;

namespace XYLC
{
    public enum XYLCAttrEnum
    {
        NoAttribute,Top=3,Bottom,Left,Right,Width,Height,CenterX,CenterY
    }

    public class XYLCAttrFlag
    {
        private UIView view1;
        private NSLayoutAttribute attr1;
        private NSLayoutRelation relation;
        private UIView view2;
        private NSLayoutAttribute attr2;
        private nfloat multiplier;
        private nfloat constant;

        public void SetValue(UIView view1, NSLayoutAttribute attr1,NSLayoutRelation relation, UIView view2,NSLayoutAttribute attr2)
        {
            this.view1 = view1;
            this.attr1 = attr1;
            this.relation = relation;
            this.view2 = view2;
            this.attr2 = attr2;
            this.multiplier = 1;
            this.constant = 0;
        }

        public XYLCAttrFlag Offset(nfloat val)
        {
            this.constant = val;
            return this;
        }

        public XYLCAttrFlag Scale(nfloat val)
        {
            this.multiplier = val;
            return this;
        }

        public NSLayoutConstraint GetConstraint()
        {
            if (view1 == null)
                return null;
            return NSLayoutConstraint.Create (view1,attr1, relation, view2, attr2, (nfloat)multiplier, constant);
        }
    }

    public class XYLCAttr
    {
        private XYLCAttrFlag flag;
        private UIView view1;
        private NSLayoutAttribute attr1;

        public XYLCAttr(UIView view1,XYLCAttrEnum attr1)
        {
            this.flag = new XYLCAttrFlag ();
            this.view1 = view1;
            this.attr1 = EnumToAttr(attr1);
        }

        private NSLayoutAttribute EnumToAttr(XYLCAttrEnum attr1)
        {
            return (NSLayoutAttribute)(int)attr1;
        }

        private XYLCAttrEnum AttrToEnum(NSLayoutAttribute attr1)
        {
            return (XYLCAttrEnum)(int)attr1;
        }

        public XYLCAttrFlag SetValue(nfloat value)
        {
            this.flag.SetValue (this.view1, this.attr1, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute);
            this.flag.Offset (value);
            return flag;
        }
        public XYLCAttrFlag EqualTo(UIView view2)
        {
            return EqualTo (view2, AttrToEnum(attr1));
        }

        public XYLCAttrFlag EqualTo(UIView view2, XYLCAttrEnum attr2)
        {
            this.flag.SetValue (this.view1, this.attr1, NSLayoutRelation.Equal, view2, EnumToAttr(attr2));
            return flag;
        }

        public NSLayoutConstraint GetConstraint()
        {
            return flag.GetConstraint ();
        }
    }

    public class XYLC
    {
        public XYLC(UIView view)
        {
            this.Left = new XYLCAttr (view, XYLCAttrEnum.Left);
            this.Right = new XYLCAttr (view,XYLCAttrEnum.Right);
            this.Top = new XYLCAttr (view,XYLCAttrEnum.Top);
            this.Bottom = new XYLCAttr (view,XYLCAttrEnum.Bottom);
            this.Width = new XYLCAttr (view,XYLCAttrEnum.Width);
            this.Height = new XYLCAttr (view,XYLCAttrEnum.Height);
            this.CenterX = new XYLCAttr (view,XYLCAttrEnum.CenterX);
            this.CenterY = new XYLCAttr (view,XYLCAttrEnum.CenterY);
        }

        public XYLCAttr Left{ get; private set;}
        public XYLCAttr Right{ get; private set; }
        public XYLCAttr Top{ get; private set; }
        public XYLCAttr Bottom{ get; private set; }
        public XYLCAttr Width{ get; private set; }
        public XYLCAttr Height{ get; private set; }
        public XYLCAttr CenterX{ get; private set; }
        public XYLCAttr CenterY{ get; private set; }

        public XYLCAttr this[int index]
        {
            get{
                return typeof(XYLC).GetProperties ()[index].GetValue(this) as XYLCAttr;
            }
        }
    }

    public static class XYLCEx
    {
        public static NSLayoutConstraint[] MakeConstraints(this UIView v,UIView rootView, Action fn)
        {
            if (v.TranslatesAutoresizingMaskIntoConstraints) 
            {
                v.TranslatesAutoresizingMaskIntoConstraints = false;
            }
            var xy = new XYLC (v);
            fn (xy);
            List list = new List ();

            for (var i = 0; i < 8; i++) 
            {
                var t = xy [i].GetConstraint ();
                if (t != null) 
                {
                    list.Add (t);
                }
            }
            if (list.Count == 0)
                return null;
            var arr = list.ToArray ();
            rootView.AddConstraints (arr);
            return arr;
        }
    }
}

你可能感兴趣的:([xamarin]山寨Masonry封装AutoLayout)