在iOS中绘制标尺-在屏幕上精确1厘米-如何绘制相距1毫米的线?
我正在尝试为任何可用的iOS设备绘制一个标尺,线条/刻度起点之间的精确距离为1mm。通常我会得到PPI并计算像素的距离。使用Objective-C,它似乎不是这样工作的在iOS中绘制标尺-在屏幕上精确1厘米-如何绘制相距1毫米的线?,ios,objective-c,ppi,Ios,Objective C,Ppi,我正在尝试为任何可用的iOS设备绘制一个标尺,线条/刻度起点之间的精确距离为1mm。通常我会得到PPI并计算像素的距离。使用Objective-C,它似乎不是这样工作的 linesDist应以“屏幕坐标像素”的形式包含我的1mm距离 有什么想法吗,我如何才能做到这一点? 我的基本代码如下所示:RulerView.m,这是一个UIView: -(void)drawRect:(CGRect)rect { [[UIColor blackColor] setFill]; float l
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} );
}
}
我不确定,但屏幕大小是以像素或点为单位计算的。你可以考虑它们中的任何一个,并做你的数学来创建一个比例尺等于或近似等于mm的代码:<代码> 1像素=0.26毫米和1点=0.35毫米。< /代码> < /P> 因此,在你的例子中,每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
iphone(除了iPhone6+之外)每英寸有163个“逻辑”点数。显然,从4开始,手机的分辨率是原来的两倍或更多,但这对坐标系没有任何影响
因此,1mm为163/25.4或约为6.4。iPad是每毫米5.2分,iPadMini和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} );
}
}
-(void)drawRect:(CGRect)rect
{
[[UIColor blackColor]setFill];
浮点数i;
浮动线DIST=163.0/25.4;//ppi/mm/in(常规尺寸的iPad为132.0)
浮动线swidthshort=15.0;
浮子线开关长=20.0;
对于(i=0,count=0;谢谢:)仍然有点不确定:我是否需要检查3种情况:iPad正常、iPhone6+、休息和调整ppi/mm?是的,您需要检查iPad的特殊情况,并且(可能)iPhone 6+。不幸的是,可靠地区分iPad和iPad mini并不是一件小事。顺便说一句,就逻辑点而言,iPhone 6似乎是154 ppi左右。
-(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} );
}
}