添加CAGradient层但无法获得渐变效果
Adding CAGradient layer but unable to get Gradient effect
这里我用这段代码来实现渐变效果,但完全没有效果。
CAGradientLayer *layer2 = [CAGradientLayer layer];
NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:@"3eb79d"].CGColor,(id)[self colorWithHexString:@"3fb65c"].CGColor,(id)[self colorWithHexString:@"3cb7da"].CGColor, nil];
[layer2 setColors:gradientColors];
[layer2 setFrame:cell.userBackgroundView.layer.frame];
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0];
cell.userBackgroundView.clipsToBounds = YES;
试试这个
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame =cell.userBackgroundView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[cell.userBackgroundView.layer insertSublayer:gradient atIndex:0];
尝试将子图层的索引从 0 更改为 -1
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1];
确保您的 userBackgroundView
未被其他视图覆盖。
使用
[layer2 setFrame:cell.userBackgroundView.bounds];
而不是
[layer2 setFrame:cell.userBackgroundView.layer.frame];
如果您使用自动布局,请确保在 viewDidAppear
或 viewDidLayOutSubviews
中设置框架
这里我用这段代码来实现渐变效果,但完全没有效果。
CAGradientLayer *layer2 = [CAGradientLayer layer];
NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:@"3eb79d"].CGColor,(id)[self colorWithHexString:@"3fb65c"].CGColor,(id)[self colorWithHexString:@"3cb7da"].CGColor, nil];
[layer2 setColors:gradientColors];
[layer2 setFrame:cell.userBackgroundView.layer.frame];
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0];
cell.userBackgroundView.clipsToBounds = YES;
试试这个
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame =cell.userBackgroundView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[cell.userBackgroundView.layer insertSublayer:gradient atIndex:0];
尝试将子图层的索引从 0 更改为 -1
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1];
确保您的 userBackgroundView
未被其他视图覆盖。
使用
[layer2 setFrame:cell.userBackgroundView.bounds];
而不是
[layer2 setFrame:cell.userBackgroundView.layer.frame];
如果您使用自动布局,请确保在 viewDidAppear
或 viewDidLayOutSubviews