Change UIButton border color on highlight(在突出显示时更改 UIButton 边框颜色)
问题描述
我有一个简单的自定义 UIButton,我添加了:
I've got a simple custom UIButton, to which I added:
button.layer.bordercolor = [[UIColor blueColor]CGColor];
但是,我想在按钮突出显示时更改 .bordercolor.我尝试向按钮的 touchDown 动作添加一个动作,将 .bordercolor 更改为红色,但是当用户抬起手指时,它会保持红色而不是返回蓝色.有什么想法吗?
However, I want to change the .bordercolor when the button is highlighted. I tried adding an action to the button's touchDown action that changes the .bordercolor to red, but when the user lifts their finger, it stays red rather than returning to blue. Any ideas?
推荐答案
你是在正确的轨道上.检查下面的代码,它对此进行了详细说明,但是您要做的是将选择器链接到按钮上的不同控件事件.一个用于 touchDown
将阴影变为红色,另一个用于 touchUpInside
在您抬起手指时将阴影变回.
You were on the right track. Check the code below, it elaborates on this, but what you'll want to do is link selectors to different control events on your button. One for touchDown
to change the shadow to red, and another for touchUpInside
to change the shadow back when you lift your finger.
此外,我看到您在 Stack Overflow 上提出了几个问题,但尚未将任何问题标记为正确答案.要继续在此网站上获得帮助,您需要开始标记问题的正确答案.
Additionally, I see you've asked several questions on Stack Overflow and have yet to mark any as the correct answer. To continue to receive help on this website, you will need to start marking correct answers to your questions.
[myButton addTarget:self action:@selector(highlightBorder) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(unhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
- (void)highlightBorder
{
myButton.layer.borderColor = [[UIColor redColor]CGColor];
}
- (void)unhighlightBorder
{
myButton.layer.borderColor = [[UIColor blueColor]CGColor];
//additional code for an action when the button is released can go here.
}
注意: UIControlEvents
的其他选项 包括:
NOTE: Other options for UIControlEvents
include:
enum {
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
};
这篇关于在突出显示时更改 UIButton 边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在突出显示时更改 UIButton 边框颜色
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01