0

最近我遇到了一个问题。我有一个 UIButton 和 2 个图像。1 表示正常状态,1 表示突出显示。问题是正常状态图像是透明的,我应该使用渐变来填充它。但如果我这样做,我突出显示的图像永远不会出现。这是代码的一部分:

    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 放在渐变前面?或者我该怎么做?

任何帮助,将不胜感激。

4

1 回答 1

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

这就是您要使其透明的方法。(更改 alpha 值)您应该在 touchdown 和 touch up 方法中更改这些值。

于 2011-12-13T09:56:32.013 回答