0

我想画一个 XoY 坐标轴...轴是 CGRects ..当设备旋转时它们会改变宽度...我希望它们在所有旋转中都保持不变。当设备处于纵向而不是横向时,宽度 5.0 是否意味着不同的东西?

这是代码:

   CGContextSaveGState(ctx);
    CGContextSetFillColorWithColor(ctx, [[UIColor whiteColor] CGColor]);

    // the axis is a rect ...
    //axis start point
    CGFloat axisStartX = viewBounds.size.width * LEFT_EXCLUSION_LENGTH_PERCENT;
    CGFloat axisStartY = viewBounds.size.height * UNDER_EXCLUSION_LENGTH_PERCENT;

    CGFloat axisLength = viewBounds .size.height - (viewBounds.size.height * OVER_EXCLUSION_LENGTH_PERCENT) - viewBounds.size.height * UNDER_EXCLUSION_LENGTH_PERCENT;
    CGContextAddRect(ctx, CGRectMake(axisStartX, axisStartY, AXIS_LINE_WIDTH, axisLength));



    CGContextFillPath(ctx);
    CGContextRestoreGState(ctx);
4

2 回答 2

2

I would not know what you are up to with this calculation, but viewBounds.size.height may change on rotation, depending on what it represents. But if its height varies for different orientations then the axisLength will vary too. And axisLength is the height of your rect.

于 2012-11-23T11:02:22.527 回答
0

If you want you rect to be of same size in every orientation than do this!

CGContextSaveGState(ctx);
CGContextSetFillColorWithColor(ctx, [[UIColor whiteColor] CGColor]);

// the axis is a rect ...
//axis start point
CGFloat boxSize = MIN(viewBounds.size.width, viewBounds.size.height);
CGFloat axisStartX = boxSize * LEFT_EXCLUSION_LENGTH_PERCENT;
CGFloat axisStartY = boxSize * UNDER_EXCLUSION_LENGTH_PERCENT;

CGFloat axisLength = boxSize - (boxSize * OVER_EXCLUSION_LENGTH_PERCENT) - boxSize * UNDER_EXCLUSION_LENGTH_PERCENT;
CGContextAddRect(ctx, CGRectMake(axisStartX, axisStartY, AXIS_LINE_WIDTH, axisLength));


CGContextFillPath(ctx);
CGContextRestoreGState(ctx);

The idea here is that whatever the orientation is, the size remains same and visible on screen.

hope that helps!

于 2012-11-23T11:02:56.177 回答