4

I'm trying to deepen my understanding of these mechanisms. I have a UIView that is touch enabled, and can update its own center property.

My understanding is that the frame property is a synthesized function of the center and bounds. Assuming that this is true, I put an observer on my touchable/movable view which observed its frame property. But that observer is never notified about the changes to frame (which happen automatically when center is changed). If I observe center directly, it works as expected.

Why doesn't observing frame work here?


Note that I know that I could be just observing center directly, which is fine. I can also surround the change to center with will/did methods for frame, which then also works:

[self willChangeValueForKey:@"frame"];
[self setCenter:center];
[self didChangeValueForKey:@"frame"];

But I mainly just want to understand why it doesn't work out of the box the way I expect it to work, in case I'm missing something conceptually here, either about KVO or about the view geometry.

Thanks.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Well, I've had luck with doing KVO on the frame. Look here for how I did it. http://stackoverflow.com/questions/4874288/how-can-i-get-a-callback-by-kvo-on-a-uiviews-frame-property – hfossli Mar 02 '12 at 16:43

2 Answers2

11

The main reason observing frame isn't working is because no UIKit property is defined to be KVO-compliant unless explicitly documented to be KVO-compliant.

You're not missing anything - Unfortunately UIKit doesn't support what you're trying to do with the observation.

Chris Parker
  • 1,252
  • 8
  • 7
4

The reason you can't observe for frame here because they are more-or-less derived values from the underlying model, the layer. All changes to frame, center, and bounds affect the layer properties. If you are keen on observing all changes to the above mentioned properties, you should observe the layer's position property.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105