Activity Indicator above Button prevents Click Recognition

帅比萌擦擦* 提交于 2019-12-23 17:51:05

问题


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


回答1:


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.




回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/8124301/activity-indicator-above-button-prevents-click-recognition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!