Can I create a totally transparent UIView that receives touches?

坚强是说给别人听的谎言 提交于 2019-12-11 06:08:54

问题


I want to create a totally transparent UIView overlay (and it has subviews) to receives touches. I could set the alpha to a low value (like 0.02) to get an approximate effect.

But I wonder is it possible for a alpha == 0 UIView to receives touches, through other UIView configs?


回答1:


You can accomplish this by overriding the hitTest:withEvent: method in the class of your fully transparent view, like:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return self;
}

The implementation of hitTest:withEvent: doesn't have to be that simple, of course. The point is that you can cause even a fully transparent view to be touchable as long as something returns that view from hitTest:withEvent:.

Do note, however, that screwing around with hitTest:withEvent: is an easy way to create some very weird bugs. Use this method with caution.




回答2:


The better way to do this is to set the background colour:

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

Add a UITapGestureRecognizer to this to hook up a selector to respond to tapping.



来源:https://stackoverflow.com/questions/17083102/can-i-create-a-totally-transparent-uiview-that-receives-touches

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