我的iPhone 4应用程序中的界面构建器中设置了一个样式为“Info Dark”的UIButton。按钮的一个属性是“突出显示”,在按钮周围显示白色高光。
我想打开和关闭此白色高亮显示,指示按钮功能是否处于活动状态。
按钮在界面构建器中使用此回调链接“Touch up inside”事件:
infoButton.highlighted = !infoButton.highlighted;
第一次触摸后,突出显示消失,并且不会像我预期的那样切换。我还需要做些什么才能使突出显示切换并显示按钮的状态?
谢谢!
更新 从界面构建器加载时,即使视图显示/消失,按钮也会保持突出显示。导致这种情况发生的原因是“显示高亮显示触摸”界面构建器属性。如果我将上面的代码分配给另一个按钮,则信息按钮会按预期高亮显示。但是,信息按钮本身的触摸会干扰上述代码,导致按钮失去“触摸”突出显示
更新2:我在界面构建器中的第一个信息按钮正下方添加了另一个信息按钮,使其永久发光。要创建切换的外观,我隐藏和取消隐藏真实下方的glowInfoButton。这按预期工作:
infoButton.highlighted = NO;
glowInfoButton.highlighted = YES;
glowInfoButton.enabled = NO;
glowInfoButton.hidden = YES;
- (IBAction)toggleInfoMode:(id)sender {
// infoButton.selected = !infoButton.selected;
glowInfoButton.hidden = !glowInfoButton.hidden;
}
答案 0 :(得分:1)
突出显示的属性不起作用,按钮不会切换。
只是知道按钮是否被按下,如果我是正确的。
如果您想实现该功能,我建议您继承UIButton或UIControl。
答案 1 :(得分:0)
也许你真正想要的是
infoButton.enabled = NO;
当设置为no时,这将使按钮变暗并禁用触摸,当设置为YES时允许正常操作。
或在你的情况下:
infoButton.enabled = !infoButton.isEnabled;
切换相同的可用性。
如果你把它放在你的touchupinside活动中,当然它只会在第一次使用。之后被禁用并且不会接收触摸事件。你可以把它放在另一个决定是否应该启用按钮的方法中。
如果您真的希望每次按下它时更改它,那么您可能应该使用开关,或者您可以查看-imageForState,-setTitle:forState和/或-setTitleColor:forState方法。如果您想在每次触摸时切换外观,您可以更改它们。
答案 2 :(得分:0)
现在,在我建议子类UIButton并检查对事件的调用然后相应地切换高亮状态之后,我看到了你真正的目的。您可以在不添加虚拟按钮的情况下执行此操作。
在自定义按钮类实现文件中放置以下代码或类似代码:
#import "HighlightedButton.h"
@implementation HighlightedButton
BOOL currentHighlightState;
-(void)toggleHighlight:(id)sender {
self.highlighted = currentHighlightState;
}
-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
//get the string indicating the action called
NSString *actionString = NSStringFromSelector(action);
//get the string for the action that you want to check for
NSString *touchUpInsideMethodName = [[self actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] lastObject];
if ([touchUpInsideMethodName isEqualToString:actionString]){
//toggle variable
currentHighlightState = !currentHighlightState;
//allow the call to pass through
[super sendAction:action to:target forEvent:event];
//toggle the property after a delay (to make sure the event has processed)
[self performSelector:@selector(toggleHighlight:) withObject:nil afterDelay:.2];
} else {
//not an event we are interested in, allow it pass through with no additional action
[super sendAction:action to:target forEvent:event];
}
}
@end
这是一个适当的解决方案,你可能不喜欢切换闪烁。我相信你是否可以解决一些可以纠正的变化。我试过了,实际上也喜欢你陈述的情况。
答案 3 :(得分:0)
UIButton的突出显示状态只是将按钮的alpha设置为0.5f。因此,如果您将按钮设置为不在高亮显示上更改,则只需在0.1和0.5之间切换Alpha。
例如:
- (void)buttonPressed:(id)sender {
if((((UIButton*)sender).alpha) != 1.0f){
[((UIButton*)sender) setAlpha:1.0f];
} else {
[((UIButton*)sender) setAlpha:0.5f];
}
}