有人见过 [UIColor initWithPatternImage] 忽略 PNG 的 alpha 值的问题吗?如果是这样,最好的解决方法是什么?
我使用 png 作为按钮对象数组的背景层,它包含图像中每个像素的多个预设 alpha 值,用作纹理背景。它可以作为图案/纹理颜色很好地加载,但它会将所有关键透明区域都设置为不透明的黑色。
获得正确的 alpha 值很重要,这样按钮图像才能正确显示。按钮框架不包括来自背景的 alpha 阴影,因为这不是按钮的“可点击”部分。此外,我的按钮对象的图像和背景图像也使用了透明度,因此它确实需要在每个按钮后面直接有一个清晰的背景,以让正确的当前颜色设置通过(最低层 UIView 将其背景颜色设置为当前用户选择的颜色)。只为包含此纹理的 UIView 层设置单个 alpha 值也不能满足我的需要。
任何帮助,将不胜感激。我目前的解决方法是使用 png 的几个 UIImageView 的完全成熟、重复编程的布局,而不是使用带有图案填充的单个 UIView。
这是一段代码,但将 UIImage 转换为 UIColor 以用作图案/纹理颜色是非常标准的:
UIView *selectorView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,320)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];
selectorView.backgroundColor = background;
[mainView addSubview:selectorView]; // pattern background layer. Add UIButtons on top of this selectorView layer
[self addSubview:mainView]; // current user selected color set in mainView.
[selectorView release];
[background release];