为什么 UIView 框架是由浮动组成的?

Why are UIView frames made up of floats?(为什么 UIView 框架是由浮动组成的?)

本文介绍了为什么 UIView 框架是由浮动组成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从技术上讲,x、y、宽度和高度表示一组与像素相关的维度.一世不能有 200.23422 像素那么为什么他们使用浮点数而不是整数?

Technically the x, y, width, and height represent a set of dimensions that relate to pixels. I can't have 200.23422 pixels so why do they use floats instead of ints?

推荐答案

浮点数的原因是现代 CPU 和 GPU 经过优化,可以并行处理许多浮点数.iOS 和 Mac 都是如此.

The reason for the floats are that modern CPUs and GPUs a are optimized to work with many floating point numbers in parallel. This is true for iOS as well as Mac.

使用 Quartz,您无需处理单个像素,但您绘制的所有内容始终是抗锯齿的.当您有一个坐标 1.0, 1.0 时,这实际上为坐标原点的 2x2 像素添加了颜色.

With Quartz you don't address individual pixels, but everything you draw is always antialiased. When you have a coordinate 1.0, 1.0 then this actually adds color to the 2x2 pixels at the coordinate origin.

这就是为什么在整数坐标处绘制时可能会出现线条模糊的原因.在非视网膜上,您必须绘制 0.5 的偏移量.从技术上讲,您需要偏移 0.25 才能在 Retina 显示器上绘制精确的像素.虽然在那里它并不重要,因为在那个像素大小上你真的看不到它了.

This is why you might get blurry lines if you draw at integer coordinates. On non-retina you habe to draw offset by 0.5. Technically you would need to offset by 0.25 to draw exact pixels on Retina displays. Though there it does not really matter that much because you don't really see it any more at that pixel size.

长话短说:您不会直接处理像素,但图形引擎会为您在浮点坐标和像素之间映射.

Long story short: you don't address pixels direcrly, but the Graphics engine maps between floating point coordinates and pixels for you.

这篇关于为什么 UIView 框架是由浮动组成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 UIView 框架是由浮动组成的?

基础教程推荐