简体   繁体   中英

Why aren't my Arcs drawing on 64 bit iOS devices?

I have code that uses the Core Foundation function CGPathAddArc() to create arcs (aka semicircles). In some cases the arcs don't draw when the code is run on 64 bit OS devices (iPad Air and Air 2, iPhone 5s, iPhone 6, etc), but they do draw on 32 bit devices.

The code looks something like this:

CGPathAddArc(path,
             nil,
             CGRectGetMidX(bounds),
             CGRectGetMidY(bounds),
             MIN(bounds.size.width/2, bounds.size.height/2),
             3*M_PI_2,
             -M_PI_2,
             _drawArcClockwise
             );

In that example, the arc draws if _drawArcClockwise is TRUE, but doesn't draw anything if _drawArcClockwise is FALSE .

I've also tested the equivalent UIBezierPath function, and it gives the same results.

You can see an example project that shows this problem on github in the repo called ArcTest (link)

The answer seems to be that there is a bug in the 64 bit implementation of iOS. (Or perhaps the bug is in the 32 bit implementation.) Regardless of which is the correct behavior, 32 bit and 64 bit devices behave differently in response to the same call with the same parameters.

The fix to the code above is to swap the startAngle and endAngle parameters when clockwise == FALSE:

CGFloat startAngle, endAngle;
startAngle = _drawArcClockwise ? 3*M_PI_2 : -M_PI_2;
endAngle =   _drawArcClockwise ? -M_PI_2  : 3*M_PI_2;
CGPathAddArc(path,
             nil,
             CGRectGetMidX(bounds),
             CGRectGetMidY(bounds),
             MIN(bounds.size.width/2, bounds.size.height/2),
             startAngle,
             endAngle,
             _drawArcClockwise
             );

That code works on both 32 and 64 bit platforms. I haven't checked to see the direction the arc is drawn on both platforms though. (Normally that doesn't matter but it would matter if you were creating an animation using the arc in a shape layer and manipulating the strokeStart and/or strokeEnd properties of the shape layer.)

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