你是否正在寻找关于setbackgroundcolor的内容?让我把最有价值的东西奉献给你:
在使用UIButton时,很多时候我们需要一个类似于- (void)setbackgroundcolor:(UIColor *)color forState:(UIControlState)state这样的方法,来实现在不同的状态下使用不同的backgroundColor,。遗憾的是,iOS默认并没有实现这个方法,那我们就自己来实现它。
让我们先来看看对于设置BackgroundImage,UIButton提供了如下方法:
-(void)setBackgroundImage:(UIImage*)imageforState:(UIControlState)stateUI_APPEARANCE_SELECTOR;//defaultisnil -(UIImage*)backgroundImageForState:(UIControlState)state;类似的,我们的函数实现声明如下:
-(void)setbackgroundcolor:(UIColor*)colorforState:(UIControlState)state; -(UIColor*)backgroundColorForState:(UIControlState)state;具体实现如下所示,代码很简单,不再赘述。
@interfaceWMButton:UIButton @property(nonatomic,copy)NSString*name; -(void)setBackgroundColor:(UIColor*)backgroundColorforState:(UIControlState)state; -(UIColor*)backgroundColorForState:(UIControlState)state; @end@implementationWMButton { NSMutableDictionary*_colors; } -(instancetype)initWithFrame:(CGRect)frame { if(self=[superinitWithFrame:frame]){ _colors=[NSMutableDictionarydictionary]; } returnself; } -(void)setBackgroundColor:(UIColor*)backgroundColorforState:(UIControlState)state { //Ifitisnormalthensetthestandardbackgroundhere if(state==UIControlStateNormal) { [supersetBackgroundColor:backgroundColor]; } //Storethebackgroundcolourforthatstate [_colorssetValue:backgroundColorforKey:[selfkeyForState:state]]; } -(UIColor*)backgroundColorForState:(UIControlState)state { return[_colorsvalueForKey:[selfkeyForState:state]]; } -(void)setHighlighted:(BOOL)highlighted { //DooriginalHighlight [supersetHighlighted:highlighted]; //HighlightwithnewcolourORreplacewithorignial NSString*highlightedKey=[selfkeyForState:UIControlStateHighlighted]; UIColor*highlightedColor=[_colorsvalueForKey:highlightedKey]; if(highlighted&&highlightedColor){ [supersetBackgroundColor:highlightedColor]; }else{ //由于系统在调用setSelected后,会再触发一次setHighlighted,故做如下处理,否则,背景色会被最后一次的覆盖掉。 if([selfisSelected]){ NSString*selectedKey=[selfkeyForState:UIControlStateSelected]; UIColor*selectedColor=[_colorsvalueForKey:selectedKey]; [supersetBackgroundColor:selectedColor]; }else{ NSString*normalKey=[selfkeyForState:UIControlStateNormal]; [supersetBackgroundColor:[_colorsvalueForKey:normalKey]]; } } } -(void)setSelected:(BOOL)selected { //DooriginalSelected [supersetSelected:selected]; //SelectwithnewcolourORreplacewithorignial NSString*selectedKey=[selfkeyForState:UIControlStateSelected]; UIColor*selectedColor=[_colorsvalueForKey:selectedKey]; if(selected&&selectedColor){ [supersetBackgroundColor:selectedColor]; }else{ NSString*normalKey=[selfkeyForState:UIControlStateNormal]; [supersetbackgroundcolor:[_colorsvalueForKey:normalKey]]; } } -(NSString*)keyForState:(UIControlState)state { return[NSStringstringWithFormat:@"state_%d",state]; } @end使用时,如下调用即可:
[buttonsetBackgroundColor:[UIColorclearColor]forState:UIControlStateNormal]; [buttonsetBackgroundColor:[UIColorclearColor]forState:UIControlStateHighlighted]; [buttonsetbackgroundcolor:HotPinkColorforState:UIControlStateSelected];需要注意的一点是,有些人可能会这样使用
[buttonsetbackgroundcolor:HotPinkColorforState:UIControlStateHighlighted|UIControlStateSelected];这种用法是不被支持的,虽然可以实现,但是,其实对于iOS默认提供方法- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil这种位于的用法也是不被完全支持的,大家可以试试看。
下面我们再来详细讨论下UIButton切换state的顺序问题。
当UIButton被按下时,会启动一个计时器,每隔一段时间,都会去检测按钮是否还处在被按下的状态。如果系统检测到它还处于被按下的状态,则就会切换到UIControlStateHighlighted,否则,恢复到UIControlStateNormal。当你长按后,在当前按钮的区域抬起手时,会切换到UIControlStateSelected,但是,需要注意的是,这次切换不仅会触发setSelected:被调用,也会触发setHighlighted:的一次调用。大家可以看看下面打印出来的Log。
2015-01-0819:43:58.782V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.227V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.277V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.294V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.327V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.344V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.377V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.494V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.528V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.577V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.660V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.777V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.894V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.927V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:00.944V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.127V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.661V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.696V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.762V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.795V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.862V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.895V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:01.930V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:03.961V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:] 2015-01-0819:44:05.162V|:highlighted=1@WMButton(#38).-[WMButtonsetHighlighted:]2015-01-08 19:44:05.328 V |: highlighted = 1 @ WMButton(#38).-[WMButton setHighlighted:]
2015-01-08 19:44:05.346 V |: selected = 1 @ WMButton(#56).-[WMButton setSelected:]
2015-01-08 19:44:05.347 V |: highlighted = 0 @ WMButton(#38).-[WMButton setHighlighted:]
以上就是关于setbackgroundcolor的全部内容,相信你一定会非常满意。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-2917-1.html