您的位置:首页 > 移动开发 > IOS开发

ios绘图函数异常

2014-06-30 14:34 190 查看
在使用ios的绘图函数时,发现坐标总是上下颠倒,原因如下

Side Effects of Drawing with Different Coordinate Systems
Some rendering oddities are brought to light when you draw an object with with reference to the default coordinate system of one drawing technology and then render it in a graphics context of the other. You may want to adjust your code to account
for these side effects.
Arcs and Rotations
If you draw a path with functions such as
CGContextAddArc and
CGPathAddArc and assume a LLO coordinate system, then you need to flip the CTM to render the arc correctly in a UIKit view. However, if you use the same function to create an arc with points located in a ULO coordinate system and
then render the path in a UIKit view, you’ll notice that the arc is an altered version of its original. The terminating endpoint of the arc now points in the opposite direction of what that endpoint would do were the arc created using the

UIBezierPath class. For example, a downward-pointing arrow now points upward (as shown in Figure 1-5), and the direction in which the arc “bends” is also different. You must change the direction of Core Graphics-drawn arcs to account
for the ULO-based coordinate system; this direction is controlled by the
startAngle and endAngle parameters of those functions.
Figure 1-5 Arc rendering in Core Graphics versus UIKit

You can observe the same kind of mirroring effect if you rotate an object (for example, by calling

CGContextRotateCTM). If you rotate an object using Core Graphics calls that make reference to a ULO coordinate system, the direction of the object when rendered in UIKit is reversed. You must account for the different directions
of rotation in your code; with CGContextRotateCTM, you do this by inverting the sign of the
angle parameter (so, for example, a negative value becomes a positive value).

上述方案要求开发者自行注意方向,另一种解决方案是多做一次y轴flip变换(from http://stackoverflow.com/questions/16974258/cgcontextaddarc-counterclockwise-instead-of-clockwise)
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, self.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

// Draw...

CGContextRestoreGState(ctx);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: