UIView 背景颜色影响 iOS 5 中的触摸

人气:720 发布:2022-10-16 标签: ipad iphone objective-c uiview uikit

问题描述

我有一个在 iOS 4 中工作的带有子类触摸响应的自定义视图.在 iOS 5 上,当沿着视图的底部边缘触摸时,这些触摸根本不会响应,如果视图的背景颜色设置为 clearColor.

I had a custom view with subclassed touch responses that was working in iOS 4. On iOS 5, these touches would not respond at all when touched along the bottom edge of the view, if the view's background color was set to clearColor.

我无法追踪到这一点,但有谁知道 iOS 5 是否改变了视图根据透明背景响应触摸的方式?

I have not been able to track this down, but does anyone know if iOS 5 changed the way views respond to touches depending on a transparent background?

除了将背景颜色设置为像 orangeColor 这样的任何不透明颜色之外,我无法对代码进行任何更改,并且视图会完全响应.

I can make no changes to the code other than set the background color to any opaque color like orangeColor and the view fully responds.

请注意,该问题不会影响视图中其他地方的触摸;仅沿底部边缘,添加到视图的最后一个子视图下方的任何位置;在查看没有内容的视图区域时,可能会为了触摸而将清晰的背景视为视图不存在.改变颜色,视图有内容"并且触摸工作!

Note the issue does not affect touches elsewhere in the view; only along the bottom edge, anywhere below the last subview added to the view; presumably a clear background is treated as if the view does not exist for the sake of touches when looking at an area of the view that has no content. Change the color, the view has "content" and the touches work!

推荐答案

不要使用[UIColor clearColor],试试这个:

[view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]];

注意:当 alpha 低于 0.1 时,UIView 不会响应触摸事件.[UIColor clearColor]alpha 设置为 0.0,因此您不会收到触摸事件.按照上面的方法,就可以在透明视图上接收到触摸事件了.

NOTE: A UIView does not respond to touch events when the alpha is anything below 0.1. [UIColor clearColor] sets an alpha to 0.0, so you won't get the touch events. Following the above method, you can receive the touch events on a transparent view.

413