iOS 白色到透明渐变层为灰色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24882361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS White to Transparent Gradient Layer is Gray
提问by Eric Gao
I have a CAGradientLayer inserted to the bottom of this small detail view that pops up at the bottom of the app. As you can see, I've set the colors from white to clear, but there's this strange gray tint that is showing up. Any ideas?
我在应用程序底部弹出的这个小细节视图的底部插入了一个 CAGradientLayer。如您所见,我已将颜色从白色设置为透明,但出现了这种奇怪的灰色调。有任何想法吗?
// Set up detail view frame and gradient
[self.detailView setFrame:CGRectMake(0, 568, 320, 55)];
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = self.detailView.bounds;
layer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
layer.startPoint = CGPointMake(1.0f, 0.7f);
layer.endPoint = CGPointMake(1.0f, 0.0f);
[self.detailView.layer insertSublayer:layer atIndex:0];
Here is the problematic view:
这是有问题的观点:
回答by Eric Gao
clearColor has a black color channel with an alpha of 0, so I had to use
clearColor 有一个黑色通道,alpha 为 0,所以我不得不使用
[UIColor colorWithWhite:1 alpha:0]
and it works fine.
它工作正常。
回答by Zaid Pathan
In SwiftThis worked for me,
在Swift这对我有用,
UIColor.white.withAlphaComponent(0).cgColor
回答by Magoo
Worth pointing out that any other colour will work like this... using a combination of the two answers above....
值得指出的是,任何其他颜色都可以这样工作......使用上面两个答案的组合......
Objective C
目标 C
UIColor *colour = [UIColor redColor];
NSArray *colourArray = @[(id)[colour colorWithAlphaComponent:0.0f].CGColor,(id)colour.CGColor]
NSArray *locations = @[@0.2,@0.8];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = colourArray;
gradientLayer.locations = locations;
gradientLayer.frame = self.frame;
[self.layer addSublayer:gradientLayer];
Swift 3
斯威夫特 3
let colour:UIColor = .red
let colours:[CGColor] = [colour.withAlphaComponent(0.0).cgColor,colour.cgColor]
let locations:[NSNumber] = [0.2,0.8]
let gradientLayer = CAGradientLayer()
gradientLayer.colors = colours
gradientLayer.locations = locations
gradientLayer.frame = frame
layer.addSublayer(gradientLayer)
回答by Stuart Casarotto
Swift 3 Syntax,
Swift 3 语法,
UIColor(white: 1, alpha: 0).cgColor
回答by AncAinu
As many, I still got a gray colour despite using a clear white.
尽管使用了清晰的白色,但我仍然得到了灰色。
So I changed my approach and went for a mask rather than a gradient. End result is the same, well, better, since this one works in all situations, not just if you got a suitable background.
所以我改变了我的方法,选择了一个面具而不是渐变。最终结果是一样的,好吧,更好,因为这适用于所有情况,而不仅仅是你有合适的背景。
I did not try this code with IB, but hopefully it works as well.
Just set backgroundColor
and you are good to go.
我没有用 IB 尝试这个代码,但希望它也能工作。只需设置backgroundColor
即可。
@IBDesignable
class FadingView: UIView {
@IBInspectable var startLocation: Double = 0.05 { didSet { updateLocations() }}
@IBInspectable var endLocation: Double = 0.95 { didSet { updateLocations() }}
@IBInspectable var horizontalMode: Bool = false { didSet { updatePoints() }}
@IBInspectable var diagonalMode: Bool = false { didSet { updatePoints() }}
@IBInspectable var invertMode: Bool = false { didSet { updateColors() }}
private let gradientLayerMask = CAGradientLayer()
private func updatePoints() {
if horizontalMode {
gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5)
gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5)
} else {
gradientLayerMask.startPoint = diagonalMode ? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0)
gradientLayerMask.endPoint = diagonalMode ? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1)
}
}
private func updateLocations() {
gradientLayerMask.locations = [startLocation as NSNumber, endLocation as NSNumber]
}
private func updateSize() {
gradientLayerMask.frame = bounds
}
private func updateColors() {
gradientLayerMask.colors = invertMode ? [UIColor.white.cgColor, UIColor.clear.cgColor] : [UIColor.clear.cgColor, UIColor.white.cgColor]
}
private func commonInit() {
layer.mask = gradientLayerMask
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
updatePoints()
updateLocations()
updateSize()
updateColors()
}
}
回答by Jake
It's worth noting that to handle white/black (or really any color with light/dark appearances) gradients based on light/dark mode in iOS 13 that this approach also works with the new system colors:
值得注意的是,要在 iOS 13 中基于亮/暗模式处理白色/黑色(或实际上任何具有亮/暗外观的颜色)渐变,这种方法也适用于新的系统颜色:
gradientLayer.colors = [
UIColor.systemBackground.cgColor,
UIColor.systemBackground.withAlphaComponent(0).cgColor]
回答by Brian Sachetta
The answers above such as:
上面的答案如:
UIColor.white.withAlphaComponent(0).cgColor
and
和
UIColor(white: 1.0, alpha: 0.0).cgColor
should work in terms of getting a portion of your gradient to be clear (rather than the gray that OP is referring to). However, if you're still seeing a transparent white when you should be seeing a clear color, make sure the backgroundColor
of the view to which you're applying the gradient is clear as well.
应该在使部分渐变清晰(而不是 OP 所指的灰色)方面起作用。但是,如果您在应该看到清晰的颜色时仍然看到透明的白色,请确保backgroundColor
应用渐变的视图的 也是清晰的。
By default, that view's background color will likely be white (or a dark color if the device is in dark mode), so when you apply the gradient, the clear portion of it will be "blocked" by the view's backgroundColor
itself. Set that to clear and you should be good to go.
默认情况下,该视图的背景颜色可能是白色(如果设备处于黑暗模式,则为深色),因此当您应用渐变时,它的清晰部分将被视图backgroundColor
本身“阻挡” 。将其设置为清除,您应该很高兴。