众所周知,...
@ElementList(name = "List")
List<ListItem> Items;
...
提供基于UIButton
的图片设定者。但似乎没有根据UIControlState
设置背景颜色
怎么做?
答案 0 :(得分:13)
从UIImage
获取UIColor
,将该图片用作按钮的背景图片。
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
[button setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateHighlighted];
答案 1 :(得分:4)
是的,这是正确的......您需要在UIButton
子类中处理此问题。
这是我使用的StyleKitButton
,UIButton
子类。它将每种模式的背景颜色存储在字典中,然后对触摸进行正确的过渡处理。
<强>用法强>
StyleKitButton *button = [[StyleKitButton alloc] init];
button.layer.borderColor = [UIColor blue].CGColor;
[button setTitleColor:[UIColor white] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blue] forState:UIControlStateNormal];
[button setTitleColor:[UIColor white] forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor purple] forState:UIControlStateHighlighted];
<强>类别:强>
#import <UIKit/UIKit.h>
@interface StyleKitButton : UIButton
@end
//------------------------------------------------------
#import "StyleKitButton.h"
@interface StyleKitButton()
@property (nonatomic, strong) NSMutableDictionary *backgroundColors;
@end
@implementation StyleKitButton
#pragma mark - Background Colors
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
if (!self.backgroundColors) {
self.backgroundColors = [[NSMutableDictionary alloc] init];
}
if (backgroundColor) {
self.backgroundColors[@(state)] = backgroundColor;
}
if (state == UIControlStateNormal) {
self.backgroundColor = backgroundColor;
}
}
- (void)transitionBackgroundToColor:(UIColor*)color {
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFade;
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.layer addAnimation:animation forKey:@"EaseOut"];
self.backgroundColor = color;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UIColor *selectedColor = self.backgroundColors[@(UIControlStateHighlighted)];
if (selectedColor) {
[self transitionBackgroundToColor:selectedColor];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
if (normalColor) {
[self transitionBackgroundToColor:normalColor];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
if (normalColor) {
[self transitionBackgroundToColor:normalColor];
}
}
@end