0

I am creating a UIView that can alter the gestureRecognizers that are added to itself. Just to test it out I am adding a tap gesture right after adding the observer to trigger the KVO.

- (void)awakeFromNib
{
    [self addObserver:self forKeyPath:@"gestureRecognizers" options:NSKeyValueObservingOptionNew context:observeContext];
    [super awakeFromNib];

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

    [self createCircles];
}


- (void)test
{
    DDLogVerbose(@"got tapped");
}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    DDLogVerbose(@"something got in here");
    if ([keyPath isEqualToString:@"gestureRecognizers"]) {
        DDLogVerbose(@"ThreeCirclesView got a gesture recognizer");
    }
}

However, it's not working. Am I doing this right? Or is the property gestureRecognizers not KVO-able?

RantriX
  • 1,017
  • 1
  • 10
  • 18

1 Answers1

1

Like most of Apple's properties, I think that gestureRecognizers is not observable. However, you can override addGestureRecognizer: in your UIView subclass so you can respond to the addition,

-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    [super addGestureRecognizer:gestureRecognizer];
    NSLog(@"added a gr");
    // do whatever in ressponse to a new gesture recognizer being added
}

You can do the same with removeGestureRecognizer: if you need to look at removal also.

rdelmar
  • 103,982
  • 12
  • 207
  • 218