简体   繁体   English

如何以编程方式为触摸状态的UIButton创建灰色叠加层?

[英]How to create a gray overlay for a UIButton in touched state programmatically?

I would like to make a generic class that when tapped, makes the element grayish. 我想创建一个泛型类,当点击时,使元素变灰。

Facebook's app is the perfect example of what I want to achieve. Facebook的应用程序是我想要实现的完美范例。 All their links and images become gray when tapped. 点击时,所有链接和图像都变为灰色。

在此输入图像描述

I can only guess that they are subclassing UIButton . 我只能猜测它们是UIButton的子类。

I have made my button's style UIButtonTypeCustom to get rid of the rounded border. 我已经使我的按钮的样式UIButtonTypeCustom摆脱了圆角边框。 Beyond this, I don't know how to have the gray overlay because I see no such property in the documentation. 除此之外,我不知道如何使用灰色叠加层,因为我在文档中看不到这样的属性。

Its simple: 这很简单:

#define TAG_GRAYVIEW 5671263 // some random number

// add the gray overlay
UIView *grayView = [[UIView alloc] initWithFrame:button.bounds];
grayView.backgroundColor = [UIColor grayColor];
grayView.tag = TAG_GRAYVIEW; 

[button addSubview:grayView];

// remove the gray overlay
UIView *grayView = [button viewWithTag:TAG_GRAYVIEW];
[grayView removeFromSuperview];

I think you need to use a semi transperant grey image PNG file. 我认为你需要使用半透明的灰色图像PNG文件。 You need to then set Image of button in Highlighted state. 然后,您需要将图像按钮设置为Highlighted状态。

Also note that both the images for Normal State and Highlighted State need to have the images with titles on them. 另请注意,“ Normal状态”和“ Highlighted状态”的图像都需要具有标题的图像。

As once we set the image to button, btn.titleLabel.text won't be displayed. 一旦我们将图像设置为按钮,就不会显示btn.titleLabel.text

So you can have a image with transperant background and title on it for Normal state. 因此对于Normal状态,您可以在其上创建具有透明背景和标题图像 And an grey image with title on it for Highlighted State. 并且带有标题灰色图像用于Highlighted状态。

Code for doing it programmatically is 以编程方式执行的代码是

[btn setImage:@"Transperant.png" forState:UIControlStateNormal];
[btn setImage:@"Grey.png" forState:UIControlStateHighlighted];

Hope this helps you. 希望这对你有所帮助。

Try setting the button up something like this. 尝试将按钮设置为这样的东西。 *mind you I didn't create the image for this example. *请注意,我没有为此示例创建图像。

Set your button type to custom, and in "State Config" select "Highlighted" from there you will want to set the image of the button to be a semi-transparent grey image. 将按钮类型设置为自定义,然后在“状态配置”中选择“突出显示”,您需要将按钮图像设置为半透明灰色图像。 There are probably several other ways to achieve this same effect but this one should be nice and simple. 可能还有其他几种方法可以达到同样的效果,但这个方法应该很简单。

在此输入图像描述

The default UIButton masks the opaque content using a gray highlight. 默认的UIButton使用灰色突出UIButton不透明内容。 For example when using a transparent png, only the parts that contain non-transparent pixels will be grayed out on touch. 例如,当使用透明png时,只有包含非透明像素的部分在触摸时才会变灰。 The default UIButton highlight has no effect on subviews/sublayers, and will only work on provided images. 默认的UIButton突出显示对子视图/子图层没有影响,并且仅对提供的​​图像有效。 What you see in the Facebook app is probably just a UIWebView highlight, possibly customized using css. 你在Facebook应用程序中看到的可能只是UIWebView的亮点,可能是使用css定制的。

To create something similar using your own control (to prevent the overhead of a UIWebView ) you should probably create your own UIControl subclass and highlight the content on touch using a transparent CALayer . 要使用您自己的控件创建类似的东西(以防止UIWebView的开销),您应该创建自己的UIControl子类并使用透明的CALayer突出显示触摸的内容。 You can even mask the CALayer to only highlight the actual contents, instead of a rectangular shape. 您甚至可以将CALayer屏蔽为仅突出显示实际内容,而不是矩形。

Also see my stackoverflow post on creating custom controls for iOS by subclassing UIControl . 另请参阅我的stackoverflow文章, 通过继承UIControl为iOS创建自定义控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM