3

我正在尝试为任何可用的 iOS 设备绘制尺子,线条/刻度之间的距离准确为 1 毫米。通常我会得到 PPI 并计算我的像素距离。使用 Objective-C,它似乎并没有以这种方式工作。

linesDist应该在“屏幕坐标像素”中包含我的 1mm 距离。

任何想法,我怎么能做到这一点?

我的基本代码如下所示: RulerView.m,它是一个 UIView:

-(void)drawRect:(CGRect)rect
{
    [[UIColor blackColor] setFill];

    float linesDist = 3.0; // 1mm * ppi ??

    float linesWidthShort = 15.0;
    float linesWidthLong = 20.0;

    for (NSInteger i = 0, count = 0; i <= self.bounds.size.height; i = i + linesDist, count++)
    {
        bool isLong = (int)i % 5 == 0;

        float linesWidth = isLong ? linesWidthLong : linesWidthShort;
        UIRectFill( (CGRect){0, i, linesWidth, 1} );
    }
}

编辑 ppi 检测(真的很难看),基于以下答案:

float ppi = 0;
switch ((int)[UIScreen mainScreen].bounds.size.height) {
    case 568: // iPhone 5*
    case 667: // iPhone 6
        ppi = 163.0;
        break;

    case 736: // iPhone 6+
        ppi = 154.0;
        break;

    default:
        return;
        break;
}
4

2 回答 2

3

iPhone(iPhone6+ 可能除外)是每英寸 163 个“逻辑”点。显然,从 4 开始的手机具有两倍或更多的分辨率,但这对坐标系没有任何影响。

因此 1 毫米是 163/25.4 或大约 6.4。iPad 为每毫米 5.2 点,iPad mini 与 iPhone 相同。

-(void)drawRect:(CGRect)rect
{
    [[UIColor blackColor] setFill];
    float i;

    float linesDist = 163.0/25.4; // ppi/mm per inch (regular size iPad would be 132.0)

    float linesWidthShort = 15.0;
    float linesWidthLong = 20.0;

    for (i = 0, count = 0; i <= self.bounds.size.height; i = i + linesDist, count++)
    {
        bool isLong = (int)count % 5 == 0;

        float linesWidth = isLong ? linesWidthLong : linesWidthShort;
        UIRectFill( (CGRect){0, i, linesWidth, 1} );
    }
} 

您想为 i 使用浮点数,以避免在添加距离时出现舍入错误并避免不必要的转换。

于 2015-09-07T12:19:21.300 回答
0

我不确定,但屏幕大小是以像素或点为单位计算的。您可以考虑其中任何一个并进行数学运算以创建一个等于或大约等于 mm 的比例1 pixel = 0.26 mm and 1 point = 0.35 mm.

所以在你的情况下,每 1 毫米画一个标记,这接近 3 分。

尝试这样的事情:

UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 1, 1)];
lineView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lineView];

// and increment the (x,y) coordinate for 3 points and draw a mark
于 2015-09-07T12:08:58.390 回答