如何设置UIButton按下后高亮状态
我有一个典型的要求,其中我需要在按下按钮后将其保持在突出显示状态。我需要执行一项仅当按钮处于突出显示状态时才有效的任务。实际上,我正在以编程方式将按钮状态设置为突出显示。
[sender setHighlighted:YES];
一旦按钮处于突出显示状态,我需要执行另一个操作。
- (IBAction)changeState: (UIButton*)sender
{
if (sender.highlighted == YES)
{
[self performSomeAtion:sender];
}
}
但是,令我恐惧的是,每当我按下任何按钮时,上述条件就会变为现实,并且该操作会被重复执行。有什么方法可以让 UIButton 的状态在按下后保持突出显示吗?
编辑 - 实际上我需要对按钮的 3 种不同状态执行 3 种不同的操作。我已经在使用选定状态和正常状态。现在,我需要利用突出显示的状态。
I have a typical requirement wherein I need to keep a button in highlighted state after pressing it. I need to perform a task which should work only when a button is in highlighted state. Actually I am setting a button state to highlighted programatically.
[sender setHighlighted:YES];
And once the button is in highlighted state i need to perform another action.
- (IBAction)changeState: (UIButton*)sender
{
if (sender.highlighted == YES)
{
[self performSomeAtion:sender];
}
}
But, to my horror, whenever I press any button, the above condition is becoming true and the action is being performed repeatedly. Is there any way in which i can keep a UIButton's state to be highlighted after pressing it?
EDIT - Actually I need to perform 3 different actions for 3 different states of the button. I am already making use of selected state and normal state. Now, I need to make use of the highlighted state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
或者您可以使用 UIButton 的两个图像(
notselectedimage.png
和selectedimage.png
)来模拟此效果,然后使用 BOOL 变量(如BOOL)跟踪按钮状态按钮当前状态;
。然后在 .h 文件中:和在 .m 文件中
or you can simulate this effect with two image for your UIButton (
notselectedimage.png
andselectedimage.png
), then keep track button state with a BOOL variable likeBOOL buttonCurrentStatus;
. Then in .h file:and in .m file
突出显示状态用于在触摸按钮时突出显示按钮。按钮中的触地事件会突出显示它。您应该改用“选定”状态。
如果您想要做的是在按下按钮后执行操作,请不要将您的方法附加到状态更改事件,而将您的方法附加到 TouchUpInside 事件。
The highlighted state is used to highlight the button while it is being touched. A touch down event in the button highlights it. You should use the "selected" state instead.
If what you want to do is perform an action after the button is pressed, don't attach your method to the state change event, attach your method to the TouchUpInside event.
我只是找到一种方法,所以我分享它,以防万一......
我保留了我的 UIButton 并为每个状态设置一个图像(这样你就可以进入 4 个状态按钮)。
我将 UserInteractionEnabled 设置为 NO ->此按钮不会受到任何触摸。
第一个按钮的目的是显示
我创建第二个自定义 UIButton 的状态,其框架与第一个按钮相同。对于此按钮,不会为该状态设置任何图像(它是一个完全透明的按钮)。该按钮的目的是捕获触摸事件。因此,我在 TouchUpInside 事件上向该按钮添加了一个目标。然后,当事件触发时,我将第一个按钮的状态更改为“禁用”、“突出显示”、“选定”或“无”这些状态(= 默认状态)。
一切都很顺利!
I just find a way, so I share it, just in case...
I kept my UIButton and set one image for each state (so you could go up to a 4 states button).
I set the UserInteractionEnabled to NO -> This button won't receive any touch.
The purpose of this first button is to show a state
I create a second custom UIButton with the same frame than the first one. For this one, none image will be set for the state (it's a fully transparent button). The purpose of this button is to catch the touch event. So I added a target to this button on the TouchUpInside event. And then when the event is fired, I change the state of the first button to Disabled, Highlighted, Selected, or none of these state (= Default state).
Everything is working like a charm!
按照您描述的方式,您最好继承 UIView 来创建您自己的三状态按钮。
实际上,您甚至应该实现自己的多状态 ButtonView,并通过用于外观的 PNG 数组和用于了解其被按下次数的状态数组来管理其内部状态。
The way you describe it, you'd be better off subclassing UIView to create your own three-state button.
Actually, you should even implement your own multistate buttonView, and manage the state it's in internally via an array of PNG for the looks and an array of states to know how many times it's been pressed.
使用
[sender setSelected: YES];
,我认为这对你有用。Use
[sender setSelected: YES];
, I think it will be useful to you.仅适用于 iOS 7:您应该考虑将图像 renderMode 设置为 UIImageRenderingModeAlwaysTemplate。然后您可以使用tintColor 来表示各种状态。
请参阅如何将tintColor应用于UIImage?
并
参阅为 UIView 及其所有子视图着色
For iOS 7 only: you should consider setting the image renderMode to UIImageRenderingModeAlwaysTemplate. You can then use the tintColor to represent various states.
see How to apply a tintColor to a UIImage?
and
see Tint a UIView with all its subviews
解决方案很棘手,但也是可能的。
问题是您尝试更改按钮操作方法中的突出显示状态,我认为这会在操作结束时进行清理或检查过程并切换突出显示状态。当您尝试调试它时,您会得到突出显示的= 1,但它最终会发生变化。
奇怪的是,当您想将按钮保持在“突出显示”模式(例如“选定”模式)以根据 3 种状态获得不同的操作时,“3 种状态按钮”有时很有用。
唯一的问题是您无法在按钮操作方法中分析它或将其切换到突出显示模式,因为当用户按下它并在最后将其切换回来时,这将立即切换到突出显示模式。
解决方案是使用调度。
这样就可以了,您可以使用 3 种状态。
The solution is tricky but it's possible.
The problem is that you tried to change the highlighted status in the button action method, which I suppose makes a clean up or check process at the end of the action and switch the highlighted status. When you try to debug it you get the highlighted = 1 but it will change at the end.
Strange but your "3 statuses button" is sometimes useful, when you'd like to keep a button in "highlighted" mode like the "selected" mode to get different action depending on the 3 statuses.
The only problem that you couldn't analyze this or switch it to highlighted mode in the button action method as this will switch to highlighted mode immediately as the user push it AND switch it back at the end.
The solution is using a dispatch.
This will do the trick and you could use the 3 statuses.
根据苹果的说法,UIButton 具有 imageView:
这意味着您可以在 IB 中(在 Storyboard 中)进行设置为此按钮设置图片并设置突出显示的图片:
button.highlighted = true
。另外,请检查配置控件下的UIControl更多状态的属性。您还可以通过编程方式执行此操作,如下所示:
Swift(在 Objective-C 中几乎相同):
According to apple, UIButton has a property of imageView:
This means that you can set in the IB (in the storyboard) a picture for this button and set the highlighted picture:
button.highlighted = true
. Also, check the UIControl under Configuring the Control’s Attributes for more states.You can also do it programatically as follows:
Swift (and almost the same in Objective-C):