【问题标题】:Handling Touch Events Of A UIView That Contains A UIButton处理包含 UIButton 的 UIView 的触摸事件
【发布时间】:2017-05-22 05:27:27
【问题描述】:

我无法使用 UIButton 子视图从 UIView 捕获触摸事件。 UIButton 可能会阻止 touchesBegantouchesEnd 事件到达外部视图。

我该如何处理这些事件?我尝试将UserInteractionEnabled 设置为false。触摸事件到达了外部视图,但UIButton 没有通过此解决方案转换为突出显示状态。

有没有办法在不丢失按钮自然行为的情况下实现这一点?

(PS 我不想使用 GestureRecognizers 等,因为我需要获取事件的实际时间戳)

【问题讨论】:

  • 如果你只是想在没有 GestureRecognizers 的情况下捕捉 UIView 的事件,你可以创建一个清晰的颜色 UIButton ,并将其作为 UIView 的框架。
  • 包含UIButtonUIView 的屏幕截图?
  • 你为什么要这样做?

标签: ios objective-c swift uibutton


【解决方案1】:

如果我理解了你的问题,你可以试试这个方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    if (CGRectContainsPoint(yourbutton.frame, [touch locationInView:self])) { //if touch is beginning on Button

    } 
}

【讨论】:

    猜你喜欢
    • 2010-12-02
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多