CALayer-AutoresizingMask 0.0.7

CALayer-AutoresizingMask 0.0.7

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年1月

Anton Bukov维护。



 
依赖
JRSwizzle>= 0
ObjcAssociatedObjectHelpers>= 0
 

为 iOS CALayers 添加 UIViewAutoresize 支持,并提供快速将 UIView 转换为 CALayer 的方法

安装

pod 'CALayer-AutoresizingMask'

使用方法

  1. 在此 storyboard 或 xib 中不使用-autolayout,并使用您需要的自动调整大小遮罩。

  2. 在 IB 中使用CALayer替换非触摸的可视视图(UIView),记得它将递归地应用于所有子视图。

  3. 使用您旧的来访问可见层:

    现在所有 CALayer 都具有 autoresizingMask 属性,类型为 UIVIewAutoresizing,您也可以使用它!

如何工作?

它只是实现了简单的算法

self.frameoriginsize 比例增加到 superviewframe增加与每个轴上可伸缩元素数量的比值。这就是全部!

CGFloat dx = self.superlayer.bounds.size.width - self.superlayerSize.width;
CGFloat dy = self.superlayer.bounds.size.height - self.superlayerSize.height;

dx /= ((mask & UIViewAutoresizingFlexibleLeftMargin)?1:0)
    + ((mask & UIViewAutoresizingFlexibleWidth)?1:0)
    + ((mask & UIViewAutoresizingFlexibleRightMargin)?1:0);
dy /= ((mask & UIViewAutoresizingFlexibleTopMargin)?1:0)
    + ((mask & UIViewAutoresizingFlexibleHeight)?1:0)
    + ((mask & UIViewAutoresizingFlexibleBottomMargin)?1:0);

CGRect frame = self.frame;
frame.origin.x += (mask & UIViewAutoresizingFlexibleLeftMargin)?dx:0;
frame.origin.y += (mask & UIViewAutoresizingFlexibleTopMargin)?dy:0;
frame.size.width += (mask & UIViewAutoresizingFlexibleWidth)?dx:0;
frame.size.height += (mask & UIViewAutoresizingFlexibleHeight)?dy:0;
self.frame = frame;

贡献

欢迎您 fork、提交pull request、创建issue...