Question

Is it possible to implement a gesture recognizer in a view and propagate it to all other UI components? If I do something like that, it doesn't work:

  UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
  swipe.direction = UISwipeGestureRecognizerDirectionRight;

  [self.view addGestureRecognizer:swipe];
  [TitleLabel addGestureRecognizer:swipe];
  [DescLabel addGestureRecognizer:swipe];
  [_TopView addGestureRecognizer:swipe];
  [_BottomView addGestureRecognizer:swipe];
  [_ScrollView addGestureRecognizer:swipe];
  [_TableView addGestureRecognizer:swipe];

  [swipe release];

How can i do it?

I need to add a transparent view on my view, that covers all objects? Or there is an intelligent way to do this?

Was it helpful?

Solution

Your findings are correct re: same recognizer to multiple components, however, for the sake of completeness...

NSInteger count = 1;
NSInteger total = [[self.view subviews] count];
for (id obj in [self.view subviews])
{
    NSLog(@"testing object %i of %i", count, total);
    count++;

    if ([obj respondsToSelector:@selector(addGestureRecognizer:)])
    {
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        [obj addGestureRecognizer: swipe];
        [swipe release];
        NSLog(@"swipe added");
    }
}

The only issue I forsee is if any of the objects you want to apply the recognizer to, are embedded inside more views that are already subviews of self.view. You'd then need to check if the found subview of self.view was of class type UIView, and if it was then to iterate through that views' subviews, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top