ios – Subviews图层转换和layoutSubviews

ios – Subviews图层转换和layoutSubviews,第1张

概述我的问题与 UIView/CALayer: Transform triggers layoutSubviews in superview 据报道,来自苹果的TSI回复: Generally, changing a geometric property of a view (or layer) will trigger a cascade of layout invalidations up the 我的问题与

UIView/CALayer: Transform triggers layoutSubviews in superview

据报道,来自苹果的TSI回复:

Generally,changing a geometric property of a vIEw (or layer) will
trigger a cascade of layout invalIDations up the vIEw hIErarchy
because parent vIEws may have auto Layout constraints involving the
modifIEd child. Note that auto Layout is active in some form
regardless of whether you have explicitly enabled it.

在我的情况下,我有一个非常简单的视图层次结构,其中视图A包含视图B.

在A的布局子视图中设置B的框架.

当我在B的背衬层上执行动画(变换和不透明)时,有时会调用A的layoutSubvIEws.这显然是一个问题,因为我在A的layoutSubvIEws中设置了B的框架,这打破了动画.

如何避免B的布局和动画之间的这种冲突?

解决方法 我发现有可能在很多方面解决这个问题:

1)临时保存变换,更改帧并重新应用变换:

[super layoutSubvIEws];//backup the @R_502_4154@ value before the layoutCA@R_502_4154@3D @R_502_4154@ = self.internalVIEw.layer.@R_502_4154@;//Set IDentity and update all the necessary framesself.internalVIEw.layer.@R_502_4154@ = CA@R_502_4154@3DIDentity;self.internalVIEw.frame = self.bounds;//set back the @[email protected].@R_502_4154@ = @R_502_4154@;

2)在不触摸框架的情况下更改视图的边界和中心.边界和中心似乎不会影响变换.有意义,因为帧是在内部计算读取图层的边界,位置,变换,锚点.

self.internalVIEw.bounds = self.bounds;self.internalVIEw.center = CGPointMake(floorf(CGRectGetWIDth(self.bounds)/2.0f),floorf(CGRectGetHeight(self.bounds)/2.0f));

3)TSI中的Apple(此处报道为UIView/CALayer: Transform triggers layoutSubviews in superview)建议

“Wrap your cell contents in an intermediate vIEw. When you modify the
@R_502_4154@ of this intermediate vIEw,only the cell’s layout should be
invalIDated,so your expensive layout method won’t be called.

If that doesn’t work,create some mechanism to signal to your
expensive layout method when it actually does (or does not) have to do
work. This Could be a property you set when the only changes you make
are to the @R_502_4154@s.”

4)我还尝试关闭自定义视图的子视图的完全自动布局.你可以从obj.io读到这篇非常好的文章,其中解释说如果从layoutSubvIEws实现中删除对[super layoutSubvIEws]的调用,你会选择自动布局来自定义视图及其所有子视图.

踩踏是你必须意识到,如果你添加一个带有约束的视图作为子视图,autolayout系统将会出现如下异常:

*** Terminating app due to uncaught exception ‘NSInternalinconsistencyException’,reason: ‘auto Layout still
required after executing -layoutSubvIEws. MyVIEw’s implementation of
-layoutSubvIEws needs to call super.’

如果你有一个简单的布局逻辑,解决方案1和2是很好的.如果你的布局逻辑是扩展的,你可以实现3或4,并裁判到另一个问题UIView/CALayer: Transform triggers layoutSubviews in superview

总结

以上是内存溢出为你收集整理的ios – Subviews图层转换和layoutSubviews全部内容,希望文章能够帮你解决ios – Subviews图层转换和layoutSubviews所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1006192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存