嗨,我在我的一个视图控制器中使用角半径 CAGradientLayer 和边框颜色在代码中制作了一个自定义按钮,如下所示:
phoneButton = [CustomButton buttonWithType:UIButtonTypeCustom];
phoneButton.frame = CGRectMake(6, 363, 99, 48);
phoneButton.titleLabel.font = [UIFont fontWithName:@"Futura-Medium" size:14];
phoneButton.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
phoneButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
[phoneButton setTitle:@"Phone" forState:UIControlStateNormal];
[phoneButton addTarget:self action:@selector(phone) forControlEvents:UIControlEventTouchUpInside];
gradient = [CAGradientLayer layer];
gradient.frame = phoneButton.bounds;
gradient.cornerRadius = 8;
gradient.borderColor = [[UIColor whiteColor]CGColor];
gradient.borderWidth = 2.0;
gradient.colors = [NSArray arrayWithObjects:(id)[[sharedManager cellGradientEnd] CGColor], (id)[[sharedManager cellGradientStart] CGColor], nil];
[phoneButton.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:phoneButton];
现在我想在选择时设置按钮的选定/突出显示颜色。我该怎么做呢。我阅读了制作 UIbutton 子类并覆盖 setSelected 但我不知道如何去做。这是customButton subclass.m
#import "CustomButton.h"
@implementation CustomButton
@synthesize sharedManager;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
sharedManager = [[MySingleton alloc]init];
}
return self;
}
-(void) setHighlighted:(BOOL)highlighted {
if(highlighted) {
NSLog(@"Highlighted");
} else {
NSLog(@"Not Highlighted");
}
[super setHighlighted:highlighted];
}
-(void) setSelected:(BOOL)selected {
if(selected) {
NSLog(@"Selected");
} else {
NSLog(@"Not Selected");
}
[super setSelected:selected];
}
@end
或者只是调暗选择按钮会很好?我应该补充一点,该按钮不在 Xib 中。