按钮上方的活动指示器阻止点击识别

发布于 2024-12-15 13:57:21 字数 337 浏览 3 评论 0原文

我有一个 UIButton“bn”和一个位于按钮上方的 UIActivityIndi​​cator“ai”(ai.center = bn.center)。

只要 ai 可见且有动画,我就无法按下 ai 框架下方的按钮,但超出了我可以按下的范围。

我是否必须将 GestureRecognition 添加到 ai 中,或者是否有更智能的方法来单击“ai”。

亲切的问候。 $h@rky

I have an UIButton "bn" and an UIActivityIndicator "ai" which is above the button (ai.center = bn.center).

As long as ai is visible and animating, I can't press the Button underneath ai's frame but out of the range I can.

Do I have to add GestureRecognition to ai or is there a smarter way to click on the "ai".

Kind regards. $h@rky

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2024-12-22 13:57:21

您可以简单地设置ai.userInteractionEnabled = NO;吗?我很惊讶它无论如何都在活动指示器上启用 - 这是一个标准组件还是您创建了一个子类?

顺便说一句,拥有一个被另一个视图覆盖的交互元素通常是糟糕的 UI 设计,特别是用于指示某些内容“忙碌”的视图,但在可点击缩略图的示例中,它似乎使感觉。

Can you simply set ai.userInteractionEnabled = NO;? I'm surprised it is enabled anyway, on an activity indicator - is this a standard component or have you made a subclass?

As an aside, it is usually poor UI design to have an interactive element that is covered by another view, particularly one which is used to indicate that something is "busy", but in your example of a clickable thumbnail image, it seems to make sense.

不忘初心 2024-12-22 13:57:21

您需要

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

在父视图中重写 UIView 方法。如果点击在按钮内,则返回按钮,否则返回 nil。这是关于此方法的讨论的一部分:

该方法通过发送
pointInside:withEvent: 向每个子视图发送消息以确定哪个
子视图应该接收触摸事件。如果pointInside:withEvent:
返回YES,则遍历子视图的层次结构;否则,其
视图层次结构的分支被忽略。你很少需要调用这个
自己方法,但是您可以覆盖它以隐藏触摸事件
子视图

编辑:

假设你有一个包含 bn 和 ai 的 UIView 子类,你可以实现这样的方法

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(bn.frame, point)) {
        return bn;
    }
    return nil;
}

,这样你的按钮将获得触摸事件(如果它们在其框架内),无论是否有东西在它上面。您不需要做任何其他事情。

You need to override the UIView method

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

in the parent view. If the hit is within the button the you return the button, else you return nil. This is part of the the discussion on this method:

This method traverses the view hierarchy by sending the
pointInside:withEvent: message to each subview to determine which
subview should receive a touch event. If pointInside:withEvent:
returns YES, then the subview’s hierarchy is traversed; otherwise, its
branch of the view hierarchy is ignored. You rarely need to call this
method yourself, but you might override it to hide touch events from
subviews
.

EDIT:

Say you have a UIView subclass which contains bn and ai, you can implement the method like this

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(bn.frame, point)) {
        return bn;
    }
    return nil;
}

that way your button will get the touch events (if they are within its frame) no matter if something is on top of it or not. You do not need to do anything else.

没有你我更好 2024-12-22 13:57:21

使用以下内容:

首先,使用以下方法重写为 UIActivityIndi​​cator 创建子类:

@interface MyActivityIndicator: UIActivityIndicator
@end

@implementation MyActivityIndicator

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return NO;
}

@end

之后,在项目中的所有位置使用 MyActivityIndi​​cator 而不是 UIActivityIndi​​cator (在 NIB 文件或代码中,取决于您在何处创建它)

Use the following stuff:

First, create subclass for UIActivityIndicator with the following method override:

@interface MyActivityIndicator: UIActivityIndicator
@end

@implementation MyActivityIndicator

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return NO;
}

@end

After that, use MyActivityIndicator everywhere in your project instead of UIActivityIndicator (in NIB file or in your code, depends where do you create it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文