Why are UIView frames made up of floats?

前端 未结 3 1365
伪装坚强ぢ
伪装坚强ぢ 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: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.

提交回复
热议问题