はじめに
背景色でカラフルにしたい時があると思います。
その際に使える、ランダムで色を作成できるコードを紹介します。
サンプルアプリ
実装
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView(.vertical) {
VStack(spacing: 5) {
ForEach(0..<50) { index in
Text("\(index)")
.frame(maxWidth: .infinity)
.frame(height: 80)
.background(Color(red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1)).gradient)
.cornerRadius(10)
}
}
}
}
}
ランダムで色を作成してる箇所
Color(red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1))
おわり
グラデーションをかけるとめっちゃいい感じになりました