gpt4 book ai didi

ios - 在 iOS 中绘制标尺 - 在屏幕上精确 1 厘米 - 如何绘制相距 1 毫米的线?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:55 24 4
gpt4 key购买 nike

我正在尝试为任何可用的 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;
}

最佳答案

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 使用 float ,以避免在添加距离时出现舍入错误并避免不必要的转换。

关于ios - 在 iOS 中绘制标尺 - 在屏幕上精确 1 厘米 - 如何绘制相距 1 毫米的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com