2013-07-01 21 views
0

我有一个UIView包含一个UIButton。
了UIView捕获使用下面的方法触摸事件:UIView处理手势,与内部UIButton,应该赶上触及

[self.view addGestureRecognizer:[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(open:)] autorelease]]; 

在某些情况下,我不想做任何事情视图时感动:

- (void) open:(UITapGestureRecognizer *)recognizer 
{ 
    if (self.someCondition == YES) return; 
    // Do very interesting stuff 
} 

的UIButton的链接到方法是这样的:

[self.deleteButton addTarget:self action:@selector(deleteTheWorld:) forControlEvents:UIControlEventTouchUpInside]; 

问题是,当someCondition = YES时,UIButton不响应触摸事件。我该如何回应?

注意:当someCondition == YES时,我只显示UIButton。

+0

尝试'tapRecognizer.cancelsTouchesInView = NO;' – danypata

回答

2

首先尝试使用tapRecognizer.cancelsTouchesInView = NO;如果这是不行的,我建议使用UIGestureRecognizerDelegate方法来防止触摸你的看法是这样的:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
     if ([touch.view isKindOfClass:[UIButton class]]) { 
       return YES; // button is touched then, yes accept the touch 
     } 
     else if (self.someContiditon == YES) { 
      return NO; // we don't want to receive touch on the view because of the condition 
     } 
     else { 
      return YES; // tap detected on view but no condition is required 
     } 
    } 
+0

tapRecognizer.cancelsTouchesInView = NO;像魅力一样工作。谢谢 ! – Oliver

0

我认为你最好的选择是管理点击你的open选择器中的按钮。

只是把像

CGPoint location = [recognizer locationInView:self.view]; 
if(self.someCondition == YES) 
    if(recognizer.state == UIGestureRecognizerStateRecognized && 
     CGRectContainsPoint(self.deleteButton.frame, location)) 
     [self deleteTheWorld]; 
    else 
     return; 

代替

- (void) open:(UITapGestureRecognizer *)recognizer 
{ 
    if (self.someCondition == YES) return; 
    // Do very interesting stuff 
} 

,当然你也不需要注册按钮的目标行动,那么!

+0

但我猜这样做,我的按钮只是简单的矩形,不会再像按钮一样,不是吗? – Oliver

+0

是啊,我说得对。我在想那个。我想你已经有了一个解决方案!顺便说一句,当我尝试时,我无法重现问题,一切都已正常工作! – micantox