我使用 UIButton 作为软键项。因此,当我单击时,我想显示按钮的颜色效果。我使用下面的代码来做到这一点。
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = btn.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:.6f] CGColor],
(id)[[UIColor colorWithRed:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:.4f] CGColor],
(id)[[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:150.0f/255.0f alpha:.4f] CGColor],
(id)[[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:.4f] CGColor],
(id)[[UIColor colorWithRed:50.0f/255.0f green:50.0f/255.0f blue:50.0f/255.0f alpha:.4f] CGColor],
(id)[[UIColor colorWithRed:5.0f/255.0f green:5.0f/255.0f blue:5.0f/255.0f alpha:.4f] CGColor],
nil];
[btn.layer insertSublayer:btnGradient atIndex:0];
CAGradientLayer *glossLayer = [CAGradientLayer layer];
glossLayer.frame = btn.bounds;
glossLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,
(id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.1f].CGColor,
nil];
glossLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:1.0f],
nil];
[btn.layer insertSublayer:glossLayer atIndex:0];
CALayer *btnLayer = [btn layer];
[btnLayer setMasksToBounds:YES];
UIColor*mycolor = btn.backgroundColor;
[btn.layer setBorderColor:[mycolor CGColor]];
尝试以下面的方式删除按钮的渐变效果。使阿尔法为零..但它没有工作..
-(void) removeGradient:(UIButton *)btn{
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = btn.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:.0f] CGColor],
(id)[[UIColor colorWithRed:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:.0f] CGColor],
(id)[[UIColor colorWithRed:150.0f/255.0f green:150.0f/255.0f blue:150.0f/255.0f alpha:.0f] CGColor],
(id)[[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:.0f] CGColor],
(id)[[UIColor colorWithRed:50.0f/255.0f green:50.0f/255.0f blue:50.0f/255.0f alpha:.0f] CGColor],
(id)[[UIColor colorWithRed:5.0f/255.0f green:5.0f/255.0f blue:5.0f/255.0f alpha:.0f] CGColor],
nil];
[btn.layer insertSublayer:btnGradient atIndex:0];
CAGradientLayer *glossLayer = [CAGradientLayer layer];
glossLayer.frame = btn.bounds;
glossLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
nil];
glossLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:1.0f],
nil];
//[btn.layer insertSublayer:glossLayer atIndex:0];
CALayer *btnLayer = [btn layer];
[btnLayer setMasksToBounds:YES];
}
在这里,如果我点击一次它工作正常。但是如果我反复点击按钮,那么颜色会重新应用到按钮上,大约 10 次点击后按钮完全变成白色。
我认为如果我们必须重做效果,我们必须删除图层的先前渐变效果。设置一次后如何去除图层上的这种渐变效果。
谢谢吉腾