Skip to content

Instantly share code, notes, and snippets.

@tomasen
Last active December 25, 2021 17:19
Show Gist options
  • Save tomasen/4ef4c6da4ac998711b765c1995c1c4a6 to your computer and use it in GitHub Desktop.
Save tomasen/4ef4c6da4ac998711b765c1995c1c4a6 to your computer and use it in GitHub Desktop.
WordCloudStateCache for WordCloudView
private var positionCache = WordCloudPositionCache()
func calcPositions(canvasSize: CGSize, itemSizes: [CGSize]) -> [CGPoint] {
var pos = [CGPoint](repeating: CGPoint.zero, count: itemSizes.count)
if canvasSize.height == 0 {
return pos
}
if positionCache.canvasSize == canvasSize {
return positionCache.positions
}
defer {
positionCache.canvasSize = canvasSize
positionCache.wordSizes = wordSizes
positionCache.positions = pos
}}
// ...
}
class WordCloudPositionCache {
var canvasSize = CGSize.zero
var wordSizes = [CGSize]()
var positions = [CGPoint]()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment