gpt4 book ai didi

ios - subview 层变换和布局 subview

转载 作者:可可西里 更新时间:2023-11-01 06:19:36 26 4
gpt4 key购买 nike

我的问题是关于

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.

在我的例子中,我有一个非常简单的 View 层次结构,其中 View A 包含 View B

A 的布局 subview 中,我正在设置 B 的框架。

当我在 B 的背衬层上执行动画(变换和不透明度)时,有时会调用 A 的 layoutSubviews。这显然是个问题,因为我在 A 的布局 subview 中设置 B 的框架,这会破坏动画。

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

最佳答案

我发现可以通过多种方式解决这个问题:

1) 临时保存变换,更改框架,然后重新应用变换:

[super layoutSubviews];

//backup the transform value before the layout
CATransform3D transform = self.internalView.layer.transform;

//Set identity and update all the necessary frames
self.internalView.layer.transform = CATransform3DIdentity;
self.internalView.frame = self.bounds;

//set back the transform
self.internalView.layer.transform = transform;

2) 在不触摸框架的情况下更改 View 的边界和中心。边界和中心似乎不影响转换。这是有道理的,因为帧是在内部计算读取层的边界、位置、变换、 anchor 。

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 transform 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 transforms."

4) 我还尝试完全关闭自定义 View subview 的自动布局。您可以从 obj.io 阅读这篇非常好的文章其中解释说,如果您从 layoutSubviews 实现中删除对 [super layoutSubviews] 的调用,您将选择退出自定义 View 及其所有 subview 的自动布局。

问题在于,您必须意识到,如果您将带有约束的 View 添加为 subview ,自动布局系统将引发如下异常:

*** 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 - subview 层变换和布局 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com