2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

背景色でカラフルにしたい時があると思います。
その際に使える、ランダムで色を作成できるコードを紹介します。

サンプルアプリ

Simulator Screen Recording - iPhone 14 Pro - 2023-06-21 at 21.45.10.gif

実装

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))

おわり

グラデーションをかけるとめっちゃいい感じになりました

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?