0

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?

P i
  • 29,020
  • 36
  • 159
  • 267

1 Answers1

1

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.

Community
  • 1
  • 1
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132