iOS:添加到视图触发器的图层(在 CADisplayLink 上更新)[view layoutSubviews] 60x/sec - 为什么?

发布于 2024-11-26 06:15:14 字数 911 浏览 6 评论 0原文

我无法弄清楚这一点。

我有一个看法。

它是一个车速表。

我在速度计中放置了一根针:

- (id) initWithFrame: (CGRect) frame
{    
    self = [super initWithFrame: frame]; // [super initWithCoder: coder];
    if (! self)
        return nil;

    :
    :
    : 
    self.needle = [ [ [Needle alloc] initWithMaxRadius: 0.5*300

    [self.layer addSublayer: needle.layer];

    self.needle.layer.position = CGPointMake( frame.size.width / 2, frame.size.height / 2 );

    return self;
}

针对象包含 CALayer 属性。

针负责为每一帧定位自身——它设置自己的显示链接处理程序并在该层上执行转换

,现在突然我的视图的layoutSubviews方法在每一帧都被命中。

我无法理解这种行为。

我可以理解 SUPERview 中的一些变化导致此方法受到影响。 但是 SUBlayer 如何强制执行此操作呢?

子层肯定应该受其父层的支配吗?

我尝试设置

self.autoresizesSubviews = NO;

没有影响,

,这显然对我正在寻找的属性

self.getsAutoresizedBySubviewsOrLayers = NO;

会发生什么?

I cannot figure this out.

I have a view.

it is a speedometer.

I put a needle in the speedometer:

- (id) initWithFrame: (CGRect) frame
{    
    self = [super initWithFrame: frame]; // [super initWithCoder: coder];
    if (! self)
        return nil;

    :
    :
    : 
    self.needle = [ [ [Needle alloc] initWithMaxRadius: 0.5*300

    [self.layer addSublayer: needle.layer];

    self.needle.layer.position = CGPointMake( frame.size.width / 2, frame.size.height / 2 );

    return self;
}

The needle object contains a CALayer property.

the needle takes care of positioning itself for every frame -- it sets up its own display link handler and performs a transform on this layer

now suddenly my view's layoutSubviews method gets hit every frame.

I cannot understand this behaviour.

I can understand some change in the SUPERview causing this method to get hit.
but how can a SUBlayer force this?

surely a sublayer should be at the mercy of its parent?

I tried setting

self.autoresizesSubviews = NO;

which obviously has no effect

the property I'm looking for would be

self.getsAutoresizedBySubviewsOrLayers = NO;

what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-12-03 06:15:14

添加子视图将导致在子视图、其目标以及目标的所有子视图上调用 layoutSubviews

您可以在问题中找到更多信息 何时调用layoutSubviews?

顺便说一句,您可能有一些上面的代码中存在内存泄漏。

如果 needle 是保留属性,则此行将导致过度保留:

self.needle = [ [ [Needle alloc] initWithMaxRadius: 0.5*300

通常,最好不要在 init 方法中引用“self.”。

Adding a subview will cause layoutSubviews to be called on the subview, it's target, and all subviews of the target.

You can find more info here in the question When is layoutSubviews called?

Incidentally, you might have some memory leaks in the code above.

If needle is a retained property, this line will cause over retention:

self.needle = [ [ [Needle alloc] initWithMaxRadius: 0.5*300

In general, it's better never to refer to "self." in an init method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文