0

我有一个包含 UIButton 的 UIView。
UIView 使用以下方法捕获触摸事件:

[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。

4

2 回答 2

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
        }
  }
于 2013-07-01T22:17:14.187 回答
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
}

当然你不需要为按钮注册目标动作!

于 2013-07-01T22:13:46.373 回答