2017-11-15 5 views
0

Ich habe die Kreisgrenze mit CAShapeLayer gefüllt. Jetzt gebe ich meine CAShape Farbe als rot, aber ich möchte es als Farbverlauf geben. Bitte sagen Sie mir, wie kann ich das tun?Füllen Sie einen CAShapeLayer mit Farbverlauf in swift?

circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(1.5 * M_PI), endAngle: CGFloat(-0.5 * M_PI), clockwise: false) 


     //add gradient to the below 
     progressCircle = CAShapeLayer() 
     progressCircle.path = circlePath?.cgPath 
     progressCircle.strokeColor = UIColor.red.cgColor 
     progressCircle.fillColor = UIColor.clear.cgColor 
     progressCircle.lineWidth = 4.0 
     progressCircle.strokeStart = 0 
     progressCircle.strokeEnd = 0.7 

     let gradient: CAGradientLayer = CAGradientLayer() 
     let startingColorOfGradient = UIColor(colorLiteralRed: 50/255, green: 189/255, blue: 170/255, alpha: 1.0).cgColor 
     let endingColorOFGradient = UIColor(colorLiteralRed: 133/255, green: 210/255, blue: 230/255, alpha: 1.0).cgColor 
     gradient.startPoint = CGPoint(x: 1.0, y: 0.5) 
     gradient.endPoint = CGPoint(x: 0.0, y: 0.5) 
     gradient.colors = [startingColorOfGradient , endingColorOFGradient] 

     circle.layer.addSublayer(progressCircle) 
circle.layer.addSublayer(gradient) 
+0

Mögliche Duplikate von [Anwenden eines Farbverlaufs auf CAShapeLayer] (https://stackoverflow.com/questions/4733966/applying-a-gradient-to-cashapelayer) –

Antwort

0

Ich denke, dass Sie den Verlaufsrahmen, die Gradientenorte fehlen und Sie sollten es der Maske der Ebene hinzufügen. Das folgende Beispiel funktioniert bei mir:

let gradient = CAGradientLayer() 
gradient.startPoint = CGPoint(x: 1, y: 0.5) 
gradient.endPoint = CGPoint(x: 0, y: 0.5) 
gradient.frame = CGRect(x: 0, y: 0, width: yourWidth, height: yourHeight) 
gradient.colors = [ 
    UIColor(white: 1, alpha: 0.1).cgColor, 
    UIColor(white: 1, alpha: 0.5).cgColor, 
    UIColor(white: 1, alpha: 0.9).cgColor 
] 
gradient.locations = [ 
    0.0, 
    0.5, 
    1.0 
] 
circle.layer.mask = gradient 

Hoffe, das hilft!

+0

Wenn ich meine Farbe als rot grün verwende, wird es nicht angezeigt irgendein Effekt. Ich stellte es Rahmen zur Verfügung, aber es zeigt nur rote Farbe – Techiee

Verwandte Themen