UIView alpha = 0 causes touches to be dropped to t

2019-02-12 12:04发布

So I created a glass pane or a custom UIView to handle touches. This glass pane sits on top of other views such as dummy UIButtons. When I set the alpha to be 0, the touches actually get intercepted by the views underneath the glass view. This is wrong. However, when I set the alpha to a low value like 0.2, the glass pane intercepts the touches.

The alpha setting was done in Interface Builder.

Anybody know how to set to the alpha to 0 and still get have this glass pane intercept touches?

2条回答
我想做一个坏孩纸
2楼-- · 2019-02-12 12:30

Yes, alpha=0.0f; and hidden=YES; have the same effect. This is very useful, because you can animate a fade-out and not have to worry about touches on the invisible object - ie you can say:

-(void)hideOverlayButton
{
    [UIView beginAnimations:@"hideOverlayButton" context:nil];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [self.overlayButton setAlpha:0.0];
    [UIVIew commitAnimations];
}

Then, when the animation is complete, the button won't respond to touches - there's no need to worry about deactivating it.

If what you want is to have invisible buttons, put a UIButton on the view, make sure it's at the front (bottom of the list in interface builder), set it's type to custom; don't set an image, background image or title. By default in interface builder you'll get alpha of 1 and a clear color.Then you can wire-up the IBOutlets in the usual way.

查看更多
Root(大扎)
3楼-- · 2019-02-12 12:32

Yes, it is standard behaviour.

For example, you can just set clear background of that UIView:

UIView *touchHandlerView;
touchHandlerView.backgroundColor = [UIColor clearColor];

In such case user wouldn't see that view - I assume that you want to do exactly that?

查看更多
登录 后发表回答