将 iPhone4 的 UIView 更新为 ScaleFactor 2.0
我有一个程序在我的 iPhone 4 上运行得很好。它使用我创建的 UIView 类(称为 TestView)来使用 Quartz 绘制图形(非常简单的图形,例如几条 CG 线条和圆圈)。然而,我需要利用 iPhone 4 上新的更高分辨率的屏幕。目前,为了确保与 iPhone 3 的向后兼容性,在代码中移动一个“点”会导致 iPhone 4 屏幕上移动 2 个像素。通过将 UIView 的比例因子更新为 2.0,您可以更改从逻辑坐标空间到像素空间的映射,以利用 iPhone 4 的更高分辨率。这应该很容易;我只是找不到任何有关如何更改 UIView 比例因子的文档。
例如,我画的任何一条线,都应该由函数 CGContextSetLineWidth(context, 1.0); 指定。 最终绘制一条 2 像素宽的线。我需要更改它以实际绘制一个像素宽。
我在苹果支持高分辨率屏幕的解释中找到了解释,但没有更改比例因子的示例或方法:
UIView文档给出了contentScaleFactor属性,但没有“set”方法。将下面的行从 ttp:// 更改为 http:// 以转换为 URL:
ttp://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref /occ/cl/UIView
I have a program running beautifully on my iPhone 4. It uses a class UIView I created (called TestView) to draw graphics using Quartz (very simply graphics such as just a few CG lines and circles). I need, however, to take advantage of the new higher resolution screen on the iPhone 4. Currently, the ensure backwards compatibility with iPhone 3, moving by a single "point" in your code causes a movement of 2 pixels on the iPhone 4 screen. By updating a UIView to a scale factor of 2.0, you can change the mapping from logical coordinate space to pixel space to take advantage of the higher resolution of the iPhone 4. It should be easy; I just can't find any documentation on how to change the UIView scale factor.
For example, any line I draw, which should is specified by the function CGContextSetLineWidth (context, 1.0);
ends up drawing a line 2 pixels wide. I need to change this to actually draw one pixel wide.
I find explanations but no examples or method for changing scale factors in Apples explanation of supporting hi res screens:
UIView documentation gives a contentScaleFactor attribute, but no "set" method. Change the line below from ttp:// to http:// to turn into URL:
ttp://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,所以 iPhone 4 足够好,可以自动将其所有 UIView 缩放为 2。更根本的问题是 CG 以不寻常的方式渲染线条(据我所知)。如果你将线宽设置为 1.0/contentScaleFactor(即 iPhone 4 为 0.5),并笔直上下(或笔直从一侧到另一侧)画一条线,一半的时间它会是一条宽度为 1 的完美、明亮的线像素。但在另一半时间,它将其渲染为一条宽度为两个像素的线,两条线的像素都只有一半亮。这在复杂的环境中看起来不错,尤其是涉及运动的环境,但如果你只是检查一条线,那就很烦人了。
解决单行问题的一个技巧是将坐标“抖动”一半。因此,如果您从 (0,0) 绘制到 (0,10) 并获得“双倍宽度”线,请尝试从 (0.5, 0) 绘制到 (0.5, 10)。
Ok, so the iPhone 4 is good enough to make all of it's UIViews automatically of scale factor 2. The more underlying problem is that CG renders lines in an unusual way (as far as I can tell). If you make the line width to be 1.0/contentScaleFactor (ie 0.5 for iPhone 4), and draw a line straight up and down (or straight side to side), half of the time it will be a perfect, bright line of width one pixel. But the other half of the time, it renders this as a line of width two pixels, with both lines of pixels being only half lit. This looks good in complicated contexts, especially that involve movement, but if you're just examining a single line, it is quite annoying.
A hack to get around this for a single line is to "jiggle" the coordinate by half a value. So if you're drawing from (0,0) to (0,10) and get the "double width" line, try drawing from (0.5, 0) to (0.5, 10).