0

嗨,我在我的一个视图控制器中使用角半径 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 中。

4

3 回答 3

1

我只是通过在 subclass.m 中创建按钮的选定渐变和未选定渐变状态来解决这个问题,现在一切都很好!

- (CustomButton *)buttonWithType:(UIButtonType)type
{
  return [self buttonWithType:UIButtonTypeCustom];
}


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

}
return self;
}

- (id)initWithCoder:(NSCoder *)coder
{
//Call the parent implementation of initWithCoder
self = [super initWithCoder:coder];

//Custom drawing methods
if (self)
{

    [self drawBackgroundLayer];
    [self drawHighlightBackgroundLayer];

    highlightBackgroundLayer.hidden = YES;


}

return self;
 }

-(void)loadSingleton{

sharedManager = [[MySingleton alloc]init];

}

- (void)layoutSubviews
{

// Set gradient frame (fill the whole button))
backgroundLayer.frame = self.bounds;

// Set inverted gradient frame
highlightBackgroundLayer.frame = self.bounds;

[super layoutSubviews];
}



- (void)drawBackgroundLayer
{
[self loadSingleton];
// Check if the property has been set already
if (!backgroundLayer)
{
    backgroundLayer = [CAGradientLayer layer];
    backgroundLayer.cornerRadius = 8;
    backgroundLayer.borderWidth = 1.5;
    backgroundLayer.borderColor = [UIColor whiteColor].CGColor;
    backgroundLayer.colors = [NSArray arrayWithObjects:(id)[[sharedManager  cellGradientEnd] CGColor], (id)[[sharedManager cellGradientStart] CGColor], nil];

    // Add the gradient to the layer hierarchy
    [self.layer insertSublayer:backgroundLayer atIndex:0];
   }
}

- (void)drawHighlightBackgroundLayer
{
[self loadSingleton];
if (!highlightBackgroundLayer)
{
    highlightBackgroundLayer = [CAGradientLayer layer];
    highlightBackgroundLayer.cornerRadius = 8;
    highlightBackgroundLayer.borderWidth = 1.5;
    highlightBackgroundLayer.borderColor = [UIColor whiteColor].CGColor;
    highlightBackgroundLayer.colors = [NSArray arrayWithObjects:(id)[[sharedManager cellSelectedGradientEnd] CGColor], (id)[[sharedManager cellSelectedGradientStart] CGColor], nil];

    [self.layer insertSublayer:highlightBackgroundLayer atIndex:1];
  }
}

并将所选状态设置为开或关

- (void)setHighlighted:(BOOL)highlighted
{
NSLog(@"Selected");

// Disable implicit animation
[CATransaction begin];
[CATransaction setDisableActions:YES];

// Hide/show inverted gradient
highlightBackgroundLayer.hidden = !highlighted;
[CATransaction commit];

[super setHighlighted:highlighted];
}
于 2013-01-30T11:21:17.310 回答
0

如果我正确理解您要做什么,我建议采用以下方法:

  1. 移动CAGradientLayer你的CustomButton实现内部(所以它会变成一个CustomGradientButton);

  2. 当您想selected为自定义按钮设置状态时,CAGradientLayer gradientColors通过更改其饱和度和亮度来更改。

通过这样做,您的按钮将更改其在选定状态下的外观。

修改饱和度和亮度的一种方法可能是通过UIColor我的这一类:

@interface UIColor (LighterDarkerColor)

- (UIColor*)colorWithSaturation:(float)saturationFactor
                 brightness:(float)brightnessFactor;

@end

@implementation UIColor (LighterDarkerColor)

- (UIColor*)colorWithSaturation:(float)saturationFactor
                 brightness:(float)brightnessFactor {

  float hue = 0.0;
  float saturation = 0.0;
  float brightness = 0.0;
  float alpha = 0.0;

  if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha])
    return [UIColor colorWithHue:hue saturation:saturation*saturationFactor
                      brightness:brightness*brightnessFactor alpha:alpha];

  return self;
}

@end

你可以这样做,例如:

UIColor* selectedColorStart = [[sharedManager cellGradientStart] colorWithSaturation:0.65 brightness:1.2];

以获得更不饱和、更亮的 cellGradientStart 颜色版本。然后在您的setSelected方法中,您将修改您的CAGradientLayer

gradient.colors = [NSArray arrayWithObjects:(id)[selectedColorEnd CGColor], (id)[selectedColorStart CGColor], nil];

这种方法对我有用,尽管您需要根据您的情况微调饱和度和亮度的选择。

于 2013-01-29T18:39:16.060 回答
0

您是否尝试过调用super然后调用setNeedsDisplay自我?

它应该会导致您的显示功能在适当的时间被调用,并且在该代码中您应该检查选择/突出显示。

于 2013-01-29T18:08:50.257 回答