我们知道,UIButton提供基于的图像设置器UIControlState.但似乎它没有提供基于的设置背景颜色UIControlState.
怎么做?
gab*_*ler 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];
Run Code Online (Sandbox Code Playgroud)
是的,这是正确的……您需要在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];
Run Code Online (Sandbox Code Playgroud)
班级:
#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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8578 次 |
| 最近记录: |