• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:14:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

任何想法,我该如何实现?

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

-(void)drawRectCGRect)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;
}



Best Answer-推荐答案


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

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

-(void)drawRectCGRect)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap