开发者

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 ||
          开发者_StackOverflow社区       touch.view == _myMenuView.menu7Btn);
    }
    return YES;
}

Regards,

KL94


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;
}


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]
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜