はじめに
貞子が出てきそうなUIをSwiftUIで作成してみます。
完成形
実装
import SwiftUI
struct ColorNoiseView: View {
let size: CGSize
var body: some View {
let colors = (0..<Int(size.width * size.height)).map { _ in
Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1))
}
GeometryReader { geometry in
ZStack {
ForEach(0..<Int(size.width * size.height), id: \.self) { index in
Rectangle()
.fill(colors[index])
.frame(width: 1, height: 1)
.position(CGPoint(x: CGFloat(index % Int(size.width)), y: CGFloat(index / Int(size.width))))
}
}
}
.frame(width: size.width, height: size.height)
}
}
使い方
import SwiftUI
struct ContentView: View {
var body: some View {
ColorNoiseView(size: .init(width: 300, height: 300))
}
}
おわり
作ってる途中に貞子出てきました