为不同状态更改UIButton的渐变

问题描述:

最近我遇到了一个问题。我有一个UIButton和2个图像。 1为正常状态,1为高亮显示。问题是正常的状态图像是透明的,我应该使用渐变填充它。但如果我这样做,我突出显示的图像从不出现。以下是代码的一部分:为不同状态更改UIButton的渐变

UIImage *balloonImage = [[UIImage imageNamed:@"balloon.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; 
    UIImage *balloonImageDown = [[UIImage imageNamed:@"balloon-down.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; 

    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = CGRectMake(0, 0, 320 - 45 - 10, 100); 
    gradient.cornerRadius = 3.0f; 
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:221.0f/255.0f green:231.0f/255.0f blue:246.0f/255.0f alpha:1.0f]CGColor], (id)[[UIColor colorWithRed:191.0f/255.0f green:207.0f/255.0f blue:234.0f/255.0f alpha:1.0f] CGColor], nil]; 
    self.balloonButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [self.balloonButton.layer insertSublayer:gradient atIndex:0]; 
    [self.balloonButton setBackgroundImage:balloonImage forState:UIControlStateNormal]; 
    [self.balloonButton setBackgroundImage:balloonImageDown forState:UIControlStateHighlighted]; 
    self.theImageView = [[[UIImageView alloc]init]autorelease]; 
    [self.balloonButton addSubview:theImageView]; 
    [self.contentView addSubview:balloonButton]; 

渐变看起来应该如此,一切正常。如果点击按钮,则更改图像。

有没有办法将balloonImageDown放在渐变的前面?或者我该怎么做?

任何帮助,将不胜感激。

+1

是的!我认为你的意思是设置一个透明层? – StackFlowed

+1

您是否考虑过将渐变颜色的alpha参数更改为低于不透明度的参数?从1.0开始0.4? – Eugene

+0

谢谢你们。这工作:) – Novarg

[yourButton setTitleColor:[UIColor colorWithRed:0.3 green:0.6 blue:0.9 alpha:0.5] 
//        set 'alpha' to something less than 1. -----^^^ 

这是你做了什么,使之透明。(改变阿尔法值) 你应该在触地改变这些值和润色方法。

+0

谢谢:)没想到那个:S – Novarg