如何在触摸UIButton时检测UIView上的触摸?

时间:2011-06-27 12:19:10

标签: iphone events uiview uibutton touch

我正在使用(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event来处理对UIView的拖拽。但是,此UIView有一些UIButtons作为UIView的子视图,当用户触及UIButton时(也在UIView上方),触摸方法不会被调用

我需要随时调用UIView中的触摸方法,并且仍然可以使用UIButton,我该如何实现?

1 个答案:

答案 0 :(得分:4)

好的,没问题,我已经解决了我的问题。

方法是覆盖按钮中的触摸方法,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchMoved = NO;
    [[self superview] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchMoved = YES;
    [[self superview] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!touchMoved) [super touchesEnded:touches withEvent:event];
}

touchMoved变量用于跟踪它是否是对按钮的直接触摸,或者触摸是否意味着拖动超级视图。当我使用UIControlEventTouchUpInside时,如果我在跟踪触摸移动时禁用了touchesEnded,它可以正常工作。

相关问题