在iOS中设置CAGradient UIButton的选定状态

时间:2013-01-29 17:21:53

标签: ios uibutton subclass cagradientlayer uicontrolevents

您好我在代码中使用角半径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中。

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

您是否尝试过调用super,然后在自己上调用setNeedsDisplay

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

答案 2 :(得分:0)

如果我理解你想要做什么,我建议采用以下方法:

  1. 移动CAGradientLayer实施内的CustomButton(因此它会成为CustomGradientButton);

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

  3. 通过执行此操作,您的按钮将在选定状态下更改其外观。

    修改饱和度和亮度的方法可以通过我的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];
    

    这种方法对我有用,虽然您需要微调您的饱和度和亮度选择,以适应您的情况。