gpt4 book ai didi

ios - 我可以为自己的控件使用 UIControlState 的自定义值吗?

转载 作者:IT王子 更新时间:2023-10-29 07:51:34 29 4
gpt4 key购买 nike

有没有办法为 UIControl 设置自定义状态——而不是现有的 UIControlState 值之一?

UIControlSate 枚举中,有 16 位可用于自定义控件状态:

UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use

问题是 UIControlstate 属性是readonly

我想为自定义状态的 UIButton 设置不同的背景图片。

最佳答案

您可以在 UIControl 的子类中使用自定义状态。

  • 创建一个名为 customState 的变量,您将在其中管理自定义状态。
  • 如果你需要设置一个状态,对这个变量做你的标记操作,然后调用[self stateWasUpdated]
  • 覆盖 state 属性以返回 [super state] 与您的 customState 的按位或运算结果
  • 覆盖 enabledselectedhighlighted setter,以便它们调用 [self stateWasUpdated]。这将允许您响应状态的任何更改,而不仅仅是对 customState
  • 的更改
  • 实现 stateWasUpdated 逻辑以响应状态变化

在标题中:

#define kUIControlStateCustomState (1 << 16)

@interface MyControl : UIControl {
UIControlState customState;
}

在实现中:

@implementation MyControl

-(void)setCustomState {
customState |= kUIControlStateCustomState;
[self stateWasUpdated];
}

-(void)unsetCustomState {
customState &= ~kUIControlStateCustomState;
[self stateWasUpdated];
}

- (UIControlState)state {
return [super state] | customState;
}

- (void)setSelected:(BOOL)newSelected {
[super setSelected:newSelected];
[self stateWasUpdated];
}

- (void)setHighlighted:(BOOL)newHighlighted {
[super setHighlighted:newHighlighted];
[self stateWasUpdated];
}

- (void)setEnabled:(BOOL)newEnabled {
[super setEnabled:newEnabled];
[self stateWasUpdated];
}

- (void)stateWasUpdated {
// Add your custom code here to respond to the change in state
}

@end

关于ios - 我可以为自己的控件使用 UIControlState 的自定义值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2856042/

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