测试子视图存在的提示

时间:2010-12-29 18:56:46

标签: iphone objective-c ipad


我会问你一个提示。 :) 我有两个视图,其中包含许多子视图(实际上它们是按钮)
当我触摸按钮时,我必须测试我的UITapGestureRecognizer触摸以禁止它。 我的代码工作得很好。但重写所有子视图以测试它们并不是很酷。

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

的问候,
KL94

2 个答案:

答案 0 :(得分:1)

如果您有一个指向包含所有按钮的视图的指针,则可以使用此代码查看该视图中的按钮是否位于该按钮上。

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

答案 1 :(得分:1)

如何定义集合:

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

然后像这样测试

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