๐Ÿžproblem with .readSize()

โญ๏ธ ๅปบ่ญฐ๏ผšๆญคๆ–นๆณ•ๆ‡‰ๆ”นๅ็‚บ .onChangeSize() ๆ‰่ƒฝ็ฌฆๅˆ็จ‹ๅผ็ขผ็š„ๅŽŸๆ„ใ€‚

โ›”๏ธ .readSize() ็š„ๅคงๅ•้กŒ๏ผš

็•ถๆœฌไพ‹ไธญ็š„ idealCellAspectRatio ๆ”น่ฎŠๆ™‚๏ผŒrows, cols, ratio ๅปๆฒ’็™ผ็”Ÿไปปไฝ•่ฎŠๅŒ–๏ผŒ ้€™ไธป่ฆๆ˜ฏๅ› ็‚บ size ไธฆๆฒ’ๆœ‰่ฎŠๅŒ–๏ผŒ็„ถ่€Œ .readSize() ่ƒŒๅพŒ็š„้‹ไฝœๆฉŸๅˆถๆ˜ฏ้€้Ž PreferenceKey (.onPreferenceChange())ไพ†ๆ“ไฝœ็š„๏ผŒ่‹ฅ size ๆฒ’ๆœ‰็™ผ็”Ÿ่ฎŠๅŒ–๏ผŒPreferenceKey ่‡ช็„ถไธๆœƒ่จญๅฎšๆ–ฐ็š„ๅ€ผ๏ผŒๅ› ๆญคไนŸไธๆœƒ่งธ็™ผ .readSize() ็š„ onChange closure๏ผŒ่ฃก้ข็š„่จˆ็ฎ—่‡ช็„ถ่ขซ่ทณ้Ž๏ผŒๆ‰€ไปฅไนŸไธๆœƒๆ›ดๆ–ฐ rows, cols, ratio ็ญ‰่ฎŠๆ•ธ โ—๏ธโ—๏ธโ—๏ธ

๐Ÿ’Š ่งฃ่—ฅ๏ผš

ไธ่ฆ้€้Ž PreferenceKey๏ผŒ็›ดๆŽฅ็”จ GeometryReaderโ—๏ธ

(่จป๏ผš็พๅœจๅทฒ็ถ“ๅŒ…่ฃๅœจ .getSize() ่ฃก้ขใ€‚)

// ๐Ÿ’Š ่งฃ่—ฅ๏ผšGeometryReader
.getSize { size in
    // recompute the current layout
    let layout = MyGrid<Int, Text>.Layout(
        idealCellAspectRatio, count: items.count, in: size
    )
    // update view states
    rows = layout.rows
    cols = layout.cols
    ratio = layout.cellSize.aspectRatio
}
problem fixed

History

  1. 2022.02.18

Last updated