【问题标题】:CALayer shadowColor not changingCALayer shadowColor 不变
【发布时间】:2015-06-20 17:25:47
【问题描述】:

所以我第一次正确设置了 UIView 图层的阴影(它显示得非常好)但是当我尝试更改颜色时(没有别的,只是 layer.shadowColor)它不会在视图中更新.它仍然显示原始颜色。我已经尝试过layer setNeedsDisplay,但没有任何运气。

self.profileIconShadow.layer.cornerRadius = self.profileIcon.frame.size.height / 2;
self.profileIconShadow.layer.shadowColor = [UIColor blackColor].CGColor;
self.profileIconShadow.layer.shadowOpacity = 1.0f;
self.profileIconShadow.layer.shadowRadius = self.profileIcon.frame.size.width * .025;
self.profileIconShadow.layer.shadowOffset = CGSizeZero;

// if ([entry[@"isInactive"] boolValue]) { // Original
if (true) { // Overrode temporarily to test it out
    self.profileIconShadow.layer.shadowColor = [UIColor redColor].CGColor;
    [self.profileIconShadow.layer setNeedsDisplay];
}

根据需要第一次正确设置阴影。但后来我尝试改变颜色,它仍然保持黑色而不是变成红色。

这就是它应该的样子。

编辑#2:我注意到如果我在第一次制作阴影时将黑色注释掉,稍后它会变为红色。我可以不多次更改shadowColor吗?

【问题讨论】:

  • 试过了,对我来说效果很好。你确定你没有在其他地方给self.profileIconShadow.layer.shadowColor分配颜色吗?
  • @0yeoj 我只有一开始将其设置为黑色然后应更改为红色的代码。

标签: ios objective-c calayer shadow


【解决方案1】:

我昨晚试了你的代码,我什至在我睡觉前用你的代码制作了开关的动画..哈哈..

问题:Can I not change shadowColor multiple times?,你当然可以.. ;)

您的图层未更新的唯一潜在原因可能是它未在主线程中执行,请确保尝试以下操作:

if ([entry[@"isInactive"] boolValue]) 
{ // Original
    dispatch_async(dispatch_get_main_queue(), ^{ 

        self.profileIconShadow.layer.shadowColor = [UIColor redColor].CGColor;

        [self.profileIconShadow.layer setNeedsDisplay];

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2021-10-19
    • 2015-09-01
    • 2014-03-22
    相关资源
    最近更新 更多