繁体   English   中英

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

[英]How to detect touch over a UIView when touched over a UIButton?

我正在使用(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ,而(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event来处理UIView上的一些拖动。 然而,这个UIView有一些UIButtons作为UIView子视图,当用户触摸UIButton (也在UIView )时,不会调用touches方法。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM