gpt4 book ai didi

ios - 选中/取消选中时 Objective-C 突出显示 UIButton

转载 作者:可可西里 更新时间:2023-11-01 05:03:41 27 4
gpt4 key购买 nike

I have a UIButton inside a UIView, when the UIButton is selected I would like have a background colour of dark gray...is this possible?

UIView *toolbar = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 40, 160)];
[toolbar setBackgroundColor:[UIColor colorWithWhite: 0.70 alpha:0.5]];

toolbar.layer.cornerRadius = 5;

UIButton *penTool = [UIButton buttonWithType:UIButtonTypeCustom];
penTool.frame = CGRectMake(5, 0, 30, 30);
[penTool setImage:[UIImage imageNamed:@"pen-but" inBundle:currentBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
[penTool addTarget:self action:@selector(drawButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
penTool.autoresizingMask = UIViewAutoresizingNone;
penTool.exclusiveTouch = YES;
penTool.tag = 1;

[toolbar addSubview:penTool];

最佳答案

如果你能做到呢?

[button setBackgroundColor:[UIColor greenColor] 
forState:UIControlStateSelected];

让我们为每个状态提供一个很棒的 AP​​I,而不仅仅是选择,就像 UIButton 默认 API 能够为每个状态更改图像和标题一样。

BGButton.h

#import <UIKit/UIKit.h>

@interface BGButton : UIButton

- (void)setBackgroundColor:(UIColor *)backgroundColor
forState:(UIControlState)state;

@end

BGButton.m

#import "BGButton.h"

@implementation BGButton {
NSMutableDictionary *_stateBackgroundColor;
}

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_stateBackgroundColor = [[NSMutableDictionary alloc] init];
}
return self;
}


- (void)setBackgroundColor:(UIColor *)backgroundColor {
[super setBackgroundColor:backgroundColor];

/*
* If user set button.backgroundColor we will set it to
* normal state's backgroundColor.
* Check if state is on normal state to prevent background being
* changed for incorrect state when updateBackgroundColor method is called.
*/
if (self.state == UIControlStateNormal) {
[self setBackgroundColor:backgroundColor forState:UIControlStateNormal];
}
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
forState:(UIControlState)state {
NSNumber *key = [NSNumber numberWithInt:state];
[_stateBackgroundColor setObject:backgroundColor forKey:key];
}

/*
* state is synthesized from other flags. So we can't use KVO
* to listen for state changes.
*/
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self stateDidChange];
}

- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
[self stateDidChange];
}

- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self stateDidChange];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self stateDidChange];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self stateDidChange];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self stateDidChange];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
[self stateDidChange];
}

- (void)stateDidChange {
[self updateBackgroundColor];
}

- (void)updateBackgroundColor {
NSNumber *key = [NSNumber numberWithInt:self.state];
self.backgroundColor = [_stateBackgroundColor objectForKey:key];
}

@end


您可以像默认的 UIButton API 一样使用它

BGButton *button = [[BGButton alloc] init];

[button setBackgroundColor:[UIColor greenColor]
forState:UIControlStateSelected];

[button setBackgroundColor:[UIColor blueColor]
forState:UIControlStateHighlighted];

[button setBackgroundColor:[UIColor orangeColor]
forState:UIControlStateDisabled];

即使你用

button.backgroundColor = [UIColor redColor];

你会保持安全,它会被翻译成

[button setBackgroundColor:[UIColor redColor] 
forState:UIControlStateNormal];

关于ios - 选中/取消选中时 Objective-C 突出显示 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com