0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【iOSアプリへの道 ②】おみくじアプリを作成してみた!!(Udemy経由)

0
Last updated at Posted at 2026-03-16

完成画面

スタート画面

おみくじ引いた後の画面

以下に学んだことをメモ

cmd + Option + Espaceキー→強制終了

swiftの命名規則
クリティカルヒットバリューのような変数名にしたい場合
criticalHitValue:ローワーキャメルケース

cmd + shift + L

めちゃくちゃ便利。
感動。
色々なオブジェクトが出てきて良い。

pythonでいうところの、関数の説明の一覧表示がされている的なニュアンス。
Imageでほしい画像などを探す時に便利。

モディファイア

  • オブジェクトに対して、色々カスタマイズできるやつ
  • 上から順番に処理される
  • オブジェクトに応じて、つけれるやつとそうでないやつがある

bool型を示す変数名

  • is
  • has
  • should

@State

  • struct内で定義した変数を、struct内で変更できるようにするため
  • 値を更新した後にViewを更新

作成したコード

味

リセットボタンを作成しました:)

//
//  ContentView.swift
//  OmikujiApp
//
//  Created by なみふみ on 2026/03/16.
//

import SwiftUI

struct ContentView: View {
    @State var omikujiresult: String = "おみくじを引こう!"
    let omikujilist = ["大吉 😆", "中吉 😊", "小吉 👌", "吉 🙂", "凶 😅", "大凶 😇"]
    
    var body: some View {
        VStack {
            // タイトル
            Text("おみくじ")
                .font(.largeTitle.bold())
                .padding()
            
            // 占い結果の表示
            Text(omikujiresult)
                .font(.title)
                .padding()
                .frame(width: 250, height: 200)
                .background(Color.yellow)
                .clipShape(RoundedRectangle(cornerRadius: 10)) // ボックスに丸みを出す
                .shadow(radius: 5) // 影
            
            // 占い実行ボタン
            Button {
                omikujiresult = omikujilist.randomElement() ?? "エラー" // おみくじが失敗したら、エラーが入る
            } label: {
                Text("おみくじを引く")
                    .font(.title2)
                    .foregroundStyle(.white)
                    .padding()
                    .background(Color.blue)
                    .clipShape(RoundedRectangle(cornerRadius: 10))
            }
            
            // リセットボタン
            Button {
                omikujiresult = "おみくじを引こう!"
            } label: {
                Text("リセットボタン")
                    .font(.title2)
                    .padding()
            }


        }.padding()
    }
}

#Preview {
    ContentView()
}

参考にしたUdemy

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?