Ich habe einen CAGradientLayer am unteren Rand dieser kleinen Detailansicht, die unten in der App angezeigt wird. Wie Sie sehen können, habe ich die Farben von weiß auf klar eingestellt, aber es gibt diesen seltsamen Grauton, der sich zeigt. Irgendwelche Ideen?
// 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];
Hier ist die problematische Ansicht:
clearColor hat einen schwarzen Farbkanal mit einem Alpha von 0, also musste ich verwenden
[UIColor colorWithWhite:1 alpha:0]
und es funktioniert gut.
In Swift Das hat für mich funktioniert,
UIColor.white.withAlphaComponent(0).cgColor
Es lohnt sich, darauf hinzuweisen, dass jede andere Farbe auf diese Weise funktionieren wird, indem eine Kombination der beiden obigen Antworten verwendet wird.
Ziel 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
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)
Swift 3 Syntax,
UIColor(white: 1, alpha: 0).cgColor
So viele wie ich, bekam ich trotz klarem Weiß immer noch eine graue Farbe.
Also änderte ich meine Herangehensweise und entschied mich für eine Maske und nicht für einen Farbverlauf. Das Endergebnis ist dasselbe, also besser, da dieses in allen Situationen funktioniert, nicht nur, wenn Sie einen geeigneten Hintergrund haben.
Ich habe diesen Code nicht mit IB ausprobiert, aber hoffentlich funktioniert es auch .. Einfach backgroundColor
setzen und schon kann es losgehen.
@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()
}
}
Es ist erwähnenswert, dass dieser Ansatz auch mit den neuen Systemfarben funktioniert, um Weiß-/Schwarz- (oder wirklich jede Farbe mit Hell-/Dunkel-Farbverläufen) zu verarbeiten, die auf dem Hell-/Dunkel-Modus in iOS 13 basieren:
gradientLayer.colors = [
UIColor.systemBackground.cgColor,
UIColor.systemBackground.withAlphaComponent(0).cgColor]