klefevre
klefevre

Reputation: 8735

A tips for testing existence of subviews


I would ask you a tips. :) I have two views which contains many subviews (in fact they are buttons)
And I have to test my UITapGestureRecognizer touches to disallow it when I touch a button. My code works very well. But it's not really cool to rewrite all subviews to testing them.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        // Disallow tapRecognizer for btn touched
        return !(touch.view == _categoriesBtn || // DISALLOW CATEGORIES BOTTOM MENU AND ITS BUTTONS
                 touch.view == _categories1Btn ||
                 touch.view == _categories2Btn ||
                 touch.view == _categories3Btn ||
                 touch.view == _myMenuView || // DISALLOW RIGHT MENU AND ITS BUTTONS
                 touch.view == _myMenuView.menu1Btn ||
                 touch.view == _myMenuView.menu2Btn ||
                 touch.view == _myMenuView.menu3Btn ||
                 touch.view == _myMenuView.menu4Btn ||
                 touch.view == _myMenuView.menu5Btn ||
                 touch.view == _myMenuView.menu6Btn ||
                 touch.view == _myMenuView.menu7Btn);
    }
    return YES;
}

Regards,
KL94

Upvotes: 0

Views: 318

Answers (2)

Nick Moore
Nick Moore

Reputation: 15857

How about defining a set:

NSSet *views=[NSSet setWithObjects:  _categoriesBtn,  _categories1Btn, /*list your views here*/ ,nil];

Then test like this

  if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        // Disallow tapRecognizer for btn touched
        return ![views containsObject:touch.view]
  }

Upvotes: 1

ughoavgfhw
ughoavgfhw

Reputation: 39925

If you have a pointer to a view containing all of the buttons, you can use this code to see if the tap is on a button in that view.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *sview; //This is the superview containing the buttons
    if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]])
        return !([touch.view isKindOfClass:[UIButton class]] && [touch.view isDescendentOfView:sview]);
    return YES;
}

Upvotes: 1

Related Questions