2017-11-15 16 views
0

저는 원호 경계를 CAShapeLayer로 채 웁니다. 이제는 내 CAShape 색상을 빨간색으로 지정하고 있지만 그래디언트 색상으로주고 싶습니다. 어떻게해야할까요?신속한 그라디언트로 CAShapeLayer 채우기?

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

[CAShapeLayer에 그라디언트 적용] 가능한 복제본 (https://stackoverflow.com/questions/4733966/applying-a-gradient-to-cashapelayer) –

답변

0

그래디언트 프레임, 그라디언트 위치가 누락되어있어 레이어의 마스크에 추가해야한다고 생각합니다. 다음 예제는 나를 위해 작동합니다 :

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 

희망이 있습니다.

+0

내 색을 빨간색 녹색으로 사용하면 표시되지 않습니다. 어떤 효과. 프레임을 제공했지만 붉은 색만 보여줍니다. – Techiee