我使用 CAGradientLayer 作为按钮的背景。
cell.showOnMap.clipsToBounds = YES;
cell.showOnMap.layer.cornerRadius = 10.0f;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.showOnMap.frame.size.width, cell.showOnMap.frame.size.height)];
CAGradientLayer *gradient2 = [CAGradientLayer layer];
gradient2.frame = view2.bounds;
gradient2.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0.00 green:0.66 blue:0.71 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:0.18 green:0.27 blue:0.75 alpha:1.0] CGColor], nil];
[cell.showOnMap.layer insertSublayer:gradient2 atIndex:0];
[cell.showOnMap bringSubviewToFront:cell.showOnMap.imageView];
在某些情况下,我的按钮showOnMap
将被禁用。在这种情况下,我希望 CAGradient 层从更改lightGrayColor
为grayColor
或完全删除该层。这是启用/禁用代码。
if(entry.address == nil)
{ [cell.showOnMap setEnabled:NO];
cell.showOnMap.layer.sublayers = nil;
}
else
[cell.showOnMap setEnabled:YES];
到目前为止,我已经尝试将整个渐变代码放入该else
部分中,然后将相同的代码放入其中,if(entry.address == nil)
但使用灰色作为渐变。这没有用;该按钮始终是最初的蓝色渐变。
我也尝试过cell.showOnMap.layer.sublayers = nil;
,但这会删除我拥有的文本和按钮图像,只留下带有圆形边缘的背景。
[[cell.showOnMap.layer.sublayers objectAtIndex:0] removeFromSuperlayer];
没有产生任何变化并[[cell.showOnMap.layer.sublayers objectAtIndex:0] removeFromSuperview];
导致崩溃
当我的按钮被禁用时,如何引用我的 CAgradient 图层并将其删除?