gpt4 book ai didi

iphone - 设置一次后如何去除UIButton的渐变效果 - iPhone

转载 作者:行者123 更新时间:2023-11-28 22:34:17 26 4
gpt4 key购买 nike

我使用 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]];

尝试通过以下方式去除按钮的渐变效果。使 alpha 为零..但它没有用..

-(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 次点击后按钮完全变成白色。

我认为如果我们必须重做效果,我们必须删除层的先前渐变效果。如何在设置一次后去除图层上的这种渐变效果。

谢谢吉森

最佳答案

你问的很常见,可以通过子类化 UIButton 轻松实现。创建子类后,将 UIResponder touchEvents 添加到子类。在 UIButton 子类中,为渐变设置一个 ivar,并在触摸开始和触摸结束时更改渐变的属性。所以在触摸开始时你可以反转它,在触摸结束时你可以将它设置回原来的渐变。

  1. 子类 UIButton
  2. 为 CAGradient 创建 ivar
  3. 在 UIButton 子类的 initWithFrame 方法中设置渐变
  4. 添加 UIResponder touch even 方法
  5. 在 touchesBegan 中 - 根据需要修改渐变。
  6. 在 touchesEnded 中重复第 5 步

我再次建议子类化,因为您需要捕获按钮的触摸事件。您不能在已设置按钮的主视图 Controller 中执行此操作。

如果这有帮助,请告诉我。

编辑 #1 - 请看下面的示例:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0f alpha:0.25f].CGColor,
(id)[UIColor colorWithWhite:0.0f alpha:0.25f].CGColor,
nil];
gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.75f],
nil];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:1.0f alpha:0.25f].CGColor,
(id)[UIColor colorWithWhite:0.0f alpha:0.25f].CGColor,
nil];
gradient.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.75f],
nil];
}

如果您注意到我正在修改现有的渐变。我不是每次都创建一个新的。我拥有的值对您不起作用,但它是不在每次点击时创建新渐变的概念。我只是在修改渐变属性。

编辑 #2 - 为您的渐变创建一个 ivar:

  1. 转到 UIButton 子类 header
  2. 在界面部分下添加:

    CAGradientLayer *btnGradient;

    CAGradientLayer *glossLayer;

关于iphone - 设置一次后如何去除UIButton的渐变效果 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16556191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com