gpt4 book ai didi

iphone - 使用渐变和阴影创建按钮的单独方法

转载 作者:可可西里 更新时间:2023-11-01 06:15:56 24 4
gpt4 key购买 nike

我写了一个给按钮添加圆角、渐变背景和阴影的方法。此方法已使用 UIButton 类编写在单独的文件 h 和 m 中,因此可以从应用程序中的任何地方调用它。我不明白为什么,当我记忆起这个方法时,按钮出现了正确的阴影和圆角,但没有正确的背景渐变。我认为问题与 self 有关,即记忆方法:按钮属性为右阴影和角,但似乎没有读取与自身相关的渐变。有人可以帮助我吗?谢谢大家。这是代码。

-(void) makeGradient {    
//corners this works
CALayer *thisLayer = self.layer;
// Add a border
thisLayer.cornerRadius = 8.0f;
thisLayer.masksToBounds = YES;
thisLayer.borderWidth = 2.0f;
thisLayer.borderColor = self.backgroundColor.CGColor;
//Gradient this doesn't work
btnGradient.cornerRadius=8.f;
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = thisLayer.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
nil];
btnGradient.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.5f],
[NSNumber numberWithFloat:0.8f],
[NSNumber numberWithFloat:1.0f],
nil];
[thisLayer addSublayer:btnGradient];

//Shadow this work

// Give it a shadow
if ([thisLayer respondsToSelector:@selector(shadowOpacity)])
{ // For compatibility, check if shadow is supported
thisLayer.shadowOpacity = 0.7;
thisLayer.shadowColor = [[UIColor blackColor] CGColor];
thisLayer.shadowOffset = CGSizeMake(0.0, 3.0);

// TODO: Need to test these on iPad
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
{
thisLayer.rasterizationScale=2.0;
}
thisLayer.shouldRasterize = YES; // FYI: Shadows have a poor effect on performance
}

最佳答案

好的,我修好了。该方法必须在 viewDidAppear 而不是 viewDidLoad 中加载!现在可以了!

关于iphone - 使用渐变和阴影创建按钮的单独方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305422/

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