Why are UIView frames made up of floats?

前端 未结 3 1351
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 05:13

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?

相关标签:
3条回答
  • 2021-01-15 05:20

    The dimensions are actually points that on non-retina screens have a 1 to 1 relation to pixels, but for retina screens 1 point = 2 pixels. So on a retina screen you can actually increment by half a point.

    0 讨论(0)
  • 2021-01-15 05:26

    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.

    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.

    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.

    0 讨论(0)
  • 2021-01-15 05:35

    Resolution independence.

    You want to keep your mathematical representation of your UI as accurate as practicable, only translating to pixel int values when you actually need to draw to the output device (and even then, not really). That's so that you can apply any number of transformations to your views and still get an accurate result.

    Moreover it is possible to render lines, for example, at half-pixel widths and even less with a visible result - the system uses intelligent antialiasing to display a fine line.

    It's the same principle as vector drawing has been using for decades (Adobe's PostScript, SVG etc). In fact Quartz is based on PDF, which is the modern version of PostScript. NeXT used Display PostScript in it's time, and then it was considered pretty revolutionary.

    0 讨论(0)
提交回复
热议问题