0

I am adding a UIView called bookViewContainer to my view controller, and I want to detect when its scale changes using KVO. Here is my viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    bookViewContainer = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:bookViewContainer];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
    [bookViewContainer addGestureRecognizer:tap];

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRecognized:)];
    [bookViewContainer addGestureRecognizer:pinch];

    [bookViewContainer addObserver:self forKeyPath:@"transform.scale" options:NSKeyValueObservingOptionNew context:NULL];
}

and my observeValueForKeyPath:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@", object);
}

However, when I run the project, I immediately get the error:

An instance 0x1d844ca0 of class NSConcreteValue was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0x1d844db0> )

I've used KVO before (but never on transform.scale), and the view is definitely not being deallocated (I'm using ARC). I've tried using scale instead, but then nothing happens. What am I doing wrong?

jowie
  • 8,028
  • 8
  • 55
  • 94

1 Answers1

0

Solution: I needed to use transform on its own, rather than a property of it. Job done!

jowie
  • 8,028
  • 8
  • 55
  • 94