2017-08-17 105 views
1

所以我有,我有UIBezierPath畫一個圓,我需要設置上海華能與我的圈子一樣,這裏是我的代碼:設置圖層蒙版UIBezierPath上海華

override func draw(_ rect: CGRect) { 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.midY), radius: circleRadius, startAngle: 0.0, endAngle:CGFloat(Double.pi * 2), clockwise: true) 
    let strokeColorAnimation = CABasicAnimation(keyPath: "strokeColor") 
    let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd") 

    shapeLayer.path = circlePath.cgPath 
    shapeLayer.lineWidth = 0.3 * circleRadius 
    shapeLayer.fillColor = UIColor.clear.cgColor 

    if oldColor == nil { oldColor = newColor } 
    if oldStrokeEnd == nil { oldStrokeEnd = 0.01 * currentCGFloatProximity } 

    strokeColorAnimation.fromValue = oldColor!.cgColor 
    strokeColorAnimation.toValue = newColor.cgColor 
    strokeColorAnimation.duration = 0.3 
    strokeColorAnimation.isRemovedOnCompletion = false 
    strokeColorAnimation.fillMode = kCAFillModeForwards 

    strokeEndAnimation.fromValue = oldStrokeEnd 
    strokeEndAnimation.toValue = 0.01 * currentCGFloatProximity 
    strokeEndAnimation.duration = 0.3 
    strokeEndAnimation.isRemovedOnCompletion = false 
    strokeEndAnimation.fillMode = kCAFillModeForwards 

    shapeLayer.add(strokeColorAnimation, forKey: strokeColorAnimation.keyPath) 
    shapeLayer.add(strokeEndAnimation, forKey: strokeEndAnimation.keyPath) 

    layer.mask = shapeLayer 


    // layer.addSublayer(shapeLayer) 


    oldColor = newColor 
    oldStrokeEnd = 0.01 * currentCGFloatProximity 
} 

這裏的時候,我加入上海華盈的是層光掩膜我shapeLayer我得到這樣的結果,它是黑色,不改變它的顏色:當我添加子層上海華盈的層我得到這樣的結果 enter image description here

//layer.mask = shapeLayer 

    layer.addSublayer(shapeLayer) 

enter image description here

我想要的只是第一張圖片的結果,但帶有彩色圓圈。希望得到幫助。對不起我的英語不好。

回答

1

好吧,我通過添加新的形狀圖層來修復它,但也許有人會有更好的解決方案。 現在我有這樣的代碼:

override func draw(_ rect: CGRect) { 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.midY), radius: circleRadius, startAngle: 0.0, endAngle:CGFloat(Double.pi * 2), clockwise: true) 
    let strokeColorAnimation = CABasicAnimation(keyPath: "strokeColor") 
    let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd") 

    circleLayer.path = circlePath.cgPath 
    circleLayer.lineWidth = 0.3 * circleRadius 
    circleLayer.fillColor = UIColor.clear.cgColor 

    backgroundLayer.path = circlePath.cgPath 
    backgroundLayer.lineWidth = 0.3 * circleRadius 
    backgroundLayer.strokeColor = UIColor.black.cgColor 
    backgroundLayer.fillColor = UIColor.clear.cgColor 


    if oldColor == nil { oldColor = newColor } 
    if oldStrokeEnd == nil { oldStrokeEnd = 0.01 * currentCGFloatProximity } 

    strokeColorAnimation.fromValue = oldColor!.cgColor 
    strokeColorAnimation.toValue = newColor.cgColor 
    strokeColorAnimation.duration = 0.3 
    strokeColorAnimation.isRemovedOnCompletion = false 
    strokeColorAnimation.fillMode = kCAFillModeForwards 

    strokeEndAnimation.fromValue = oldStrokeEnd 
    strokeEndAnimation.toValue = 0.01 * currentCGFloatProximity 
    strokeEndAnimation.duration = 0.3 
    strokeEndAnimation.isRemovedOnCompletion = false 
    strokeEndAnimation.fillMode = kCAFillModeForwards 

    circleLayer.add(strokeColorAnimation, forKey: strokeColorAnimation.keyPath) 
    circleLayer.add(strokeEndAnimation, forKey: strokeEndAnimation.keyPath) 

    layer.mask = backgroundLayer 

    layer.addSublayer(circleLayer) 
    oldColor = newColor 
    oldStrokeEnd = 0.01 * currentCGFloatProximity 
} 

Result