简体   繁体   中英

iOS - CAShapeLayer.strokeEnd does not seem to work

I am currently trying to draw a segment of an arc.

I managed to draw the segment, but now I want to control the length of the segment via the strokeEnd property but it does not work.

Here is my code:

    let arc:CGMutablePathRef = CGPathCreateMutable()
    CGPathAddArc(arc, nil, arcCenter.x, arcCenter.y, arcDiameter/2, arcStartAngle, arcStopAngle, false)
    let arcLineWidth = 5.0
    let strokedArc:CGpathRef = CGPathCreateCopyByStrokingPath(arc, nil, arcLineWidth, kcGLineCapButt, kCGJoinMiter, 10)
    arcLayer.path = strokedArc
    arcLayer.fillColor = arcColor
    arcView.layer.addsublayer (arcLayer)

    ...

    self.addSubview(arcView)

I then try to modify the length of the segment:

    arcLayer.strokeEnd = 0.5

But it does not do anything.

Can you please tell me what I am doing wrong?

Thanks,

MG

You are creating a new path by stroking the original path and then you are assigning the new path to the shape layer and filling it.

From the code that you have supplied in your question, you are only configuring the shape layer to fill the shape, not to stroke it. Thus there is no stroke to be affected by the strokeEnd property.


To have the strokeEnd work on the original arc path. Assign the original arc path to the shape layer and configure the stroke color, line width, etc. on the shape layer. If you don't want to fill the arc you will also have to set the fill color to a clear color (I seem to remember that it works if you set it to nil in Swift).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM