IEには対応していません。Edgeでサイトを開く ≫

ソースコード source code

下記アプリの主要なソースコードを公開しています。アプリ開発の参考になれば幸いです。

画像等が別途必要ですので下記情報のみでアプリが完成するものではありません。 アプリは少しずつ機能拡張していますのでストア公開されているアプリと内容が異なる場合があります。 コードはコピーして自由にお使いいただけます。ただし著作権は放棄しておりませんので全部の再掲載はご遠慮ください。部分的に再掲載したり、改変して再掲載するのは構いません。 自身のアプリ作成の参考として個人使用・商用問わず自由にお使いいただけます。 コード記述のお手本を示すものではありません。ミニアプリですので変数名などさほど気遣いしていない部分も有りますし間違いも有るかと思いますので参考程度にお考え下さい。 他の賢者の皆様が公開されているコードを参考にした箇所も含まれます。iOSアプリ開発の熟練者が書いたコードではありません。 エンジニア向け技術情報共有サービスではありませんので説明は省いています。ご了承ください。 GitHubなどへの公開は予定しておりません。

下記コードの最終ビルド日: 2021-04-23

ConstValue.swift

//
//  ConstValue.swift
//  Roulette
//
//  Created by akira ohmachi on 2021/04/03.
//

import SwiftUI

class ConstValue: ObservableObject {
    
    //background
    static let colorBg: Color = Color.init(red:0.59,green:0.51,blue:0.33)
    
    //button color
    static let colorStart: Color = Color.init(red:0.238, green:0.77, blue:0.387)
    static let colorSetting: Color = Color.init(red:0.543, green:0.77, blue:0.289)
    
    static let colorArcArray: Array<Color> = [
            Color.init(red: 234 / 256, green: 123 / 256, blue: 132 / 256),
            Color.init(red: 240 / 256, green: 196 / 256, blue: 123 / 256),
            Color.init(red: 247 / 256, green: 239 / 256, blue: 123 / 256),
            Color.init(red: 192 / 256, green: 217 / 256, blue: 139 / 256),
            Color.init(red: 123 / 256, green: 197 / 256, blue: 156 / 256),
            Color.init(red: 123 / 256, green: 201 / 256, blue: 235 / 256),
            Color.init(red: 123 / 256, green: 173 / 256, blue: 211 / 256),
            Color.init(red: 138 / 256, green: 139 / 256, blue: 189 / 256),
            Color.init(red: 194 / 256, green: 127 / 256, blue: 186 / 256),
            Color.init(red: 233 / 256, green: 123 / 256, blue: 185 / 256),
    ]
    static let colorArcs: Array<Color> = [
        colorArcArray[0],
        colorArcArray[2],
        colorArcArray[4],
        colorArcArray[6],
        colorArcArray[8],
        colorArcArray[1],
        colorArcArray[3],
        colorArcArray[5],
        colorArcArray[7],
        colorArcArray[9],
        colorArcArray[0],
        colorArcArray[2],
        colorArcArray[4],
        colorArcArray[6],
        colorArcArray[8],
        colorArcArray[1],
        colorArcArray[3],
        colorArcArray[5],
        colorArcArray[7],
        colorArcArray[9],
    ]

    static let colorArcDarkArray: Array<Color> = [
            Color.init(red: 222 / 256, green:   0 / 256, blue:  17 / 256),
            Color.init(red: 234 / 256, green: 145 / 256, blue:   0 / 256),
            Color.init(red: 247 / 256, green: 232 / 256, blue:   0 / 256),
            Color.init(red: 137 / 256, green: 188 / 256, blue:  30 / 256),
            Color.init(red:   0 / 256, green: 147 / 256, blue:  66 / 256),
            Color.init(red:   0 / 256, green: 154 / 256, blue: 225 / 256),
            Color.init(red:   0 / 256, green: 101 / 256, blue: 176 / 256),
            Color.init(red:  28 / 256, green:  31 / 256, blue: 131 / 256),
            Color.init(red: 140 / 256, green:   7 / 256, blue: 126 / 256),
            Color.init(red: 220 / 256, green:   0 / 256, blue: 123 / 256),
	]

    static let colorArcDarks: Array<Color> = [
        colorArcDarkArray[0],
        colorArcDarkArray[2],
        colorArcDarkArray[4],
        colorArcDarkArray[6],
        colorArcDarkArray[8],
        colorArcDarkArray[1],
        colorArcDarkArray[3],
        colorArcDarkArray[5],
        colorArcDarkArray[7],
        colorArcDarkArray[9],
        colorArcDarkArray[0],
        colorArcDarkArray[2],
        colorArcDarkArray[4],
        colorArcDarkArray[6],
        colorArcDarkArray[8],
        colorArcDarkArray[1],
        colorArcDarkArray[3],
        colorArcDarkArray[5],
        colorArcDarkArray[7],
        colorArcDarkArray[9],
    ]

}

ContentView.swift

//
//  ContentView.swift
//  Roulette
//
//  Created by akira ohmachi on 2021/04/03.
//

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var pub: PublicManager
    @State private var choiceResult: String = "result"
    @State private var busyFlag: Bool = false
    @State private var destroyFlag: Bool = false
    @State private var colorBg: Color = Color.white
    @State private var itemRateSum: Double = 0.0
    @State private var currentItems: Array<PublicManager.Item> = []
    @State private var actionState: Int = 0
    @State private var actionCount: Int = 0
    @State private var actionAngle: Double = 0.0
    struct ArcState {
        var start: Double
        var end: Double
        var color1: Color
        var color2: Color
        var name: String
        init(start: Double, end: Double, color1: Color, color2: Color, name: String) {
            self.start = start
            self.end = end
            self.color1 = color1
            self.color2 = color2
            self.name = name
        }
        func isDraw() -> Bool {
            if self.start == 0.0 && self.end == 0.0 {
                return false
            }
            return true
        }
        mutating func setHidden() {
            self.start = 0.0
            self.end = 0.0
        }
    }
    //start=0,end=0の場合は描画しない
    @State private var arcs: Array<ArcState> = [
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),

        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
        ArcState(start: 0, end: 0, color1: Color.black, color2: Color.black, name: ""),
    ]
    
    //doubleを入力用stringに変換
    func itemRatioValue(num: Double) -> String {
        if num == 0.0 {
            return ""
        } else {
            let numInt: Int = Int(num)
            if Double(numInt) == num {
                return String(numInt)
            } else {
                return String(num)
            }
        }
    }
    
    var body: some View {
        NavigationView {
            GeometryReader { bodyView in
                VStack(spacing: 0) {
                    HStack(spacing: 0) {
                        Button(action:{
                            self.startRoulette()
                        }){
                            Text("start")
                                .frame(width: bodyView.size.width / 4 * 3, height: bodyView.size.height / 6, alignment: .center)
                                .background(ConstValue.colorStart)
                                .foregroundColor(Color.white)
                        }
                        Rectangle().fill(Color.white).frame(width: 1, height: bodyView.size.height / 6)
                        NavigationLink(destination: SettingView(),isActive: self.$pub.isSetting) {
                            Button(action:{
                                if (self.busyFlag) {
                                    return
                                }
                                for i in 0..<self.pub.items.count {
                                    self.pub.tmpItems[i].name = self.pub.items[i].name
                                    self.pub.tmpItems[i].ratio = self.itemRatioValue(num: self.pub.items[i].ratio)
                                }
                                self.pub.tmpItemSplit = self.pub.itemSplit
                                self.pub.tmpItemReverse = self.pub.itemReverse
                                self.pub.isSetting = true
                            }){
                                Text("setting")
                                    .frame(width: bodyView.size.width / 4, height: bodyView.size.height / 6, alignment: .center)
                                    .background(ConstValue.colorSetting)
                                    .foregroundColor(Color.white)
                            }
                        }
                    }
                    .frame(width: bodyView.size.width, height: bodyView.size.height / 6)
                    Rectangle().fill(Color.white).frame(width: bodyView.size.width, height: 1)
                    ZStack(alignment: .bottom) {
                        ScrollView {
                            Spacer(minLength: bodyView.size.height / 40)
                            Text(self.choiceResult).animation(.none)
                            Spacer(minLength: bodyView.size.height / 40)
                            ZStack {
                                self.baseArc(size: bodyView.size.width)
                                ForEach(0..<self.arcs.count) {
                                    if self.arcs[$0].isDraw() {
                                        self.fillArc(start: self.arcs[$0].start, end: self.arcs[$0].end, color1: self.arcs[$0].color1, color2: self.arcs[$0].color2, size: bodyView.size.width)
                                    }
                                }
                                ForEach(0..<self.arcs.count) {
                                    if self.arcs[$0].isDraw() {
                                        self.arcText(start: self.arcs[$0].start, end: self.arcs[$0].end, name: self.arcs[$0].name, size: bodyView.size.width)
                                    }
                                }
                            }
                            Spacer(minLength: 150)
                        }
                        HStack(spacing: 0) {
                            Spacer(minLength: 0)
                            PublicManager.AdView().frame(maxWidth: bodyView.size.width, maxHeight: 50)
                            Spacer(minLength: 0)
                        }.background(Color(red: 0.2,green: 0.2,blue: 0.2))
                    }
                }
                .background(self.colorBg)
                //.navigationBarTitle("")
                .navigationBarHidden(true)
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .onAppear {     //アプリ起動時に実行
            self.pub.loadItems()
            self.initItemData()
            self.drawRoulette()
        }
        .onChange(of: self.pub.isSetting) { _ in    //SettingViewからの戻り
            if self.pub.isSetting == false {
                self.initItemData()
                self.drawRoulette()
            }
        }
        .onDisappear {     //アプリ終了時
            self.destroyFlag = true
        }
    }

    private func startRoulette() {
        if self.busyFlag {
            return
        }
        self.busyFlag = true
        self.initItemData()
        self.actionState = 0
        self.actionCount = 0
        self.actionTimeline()
    }

    private func initItemData() {
        self.itemRateSum = 0.0
        self.currentItems = []
        for i in 0..<self.pub.items.count {
            if self.pub.items[i].name != "" {
                self.currentItems.append(self.pub.items[i])
                self.itemRateSum += self.pub.items[i].ratio
            }
        }
        if self.pub.itemSplit { //4倍にするか
            for _ in 0..<3 {    //3回同じことを繰り返す
                for i in 0..<self.pub.items.count {
                    if self.pub.items[i].name != "" {
                        self.currentItems.append(self.pub.items[i])
                        self.itemRateSum += self.pub.items[i].ratio
                    }
                }
            }
        }
        if self.pub.itemReverse {
            self.currentItems = self.currentItems.reversed()
        }
        //Viewの状態を初期化
        for i in 0..<self.arcs.count {
            self.arcs[i].setHidden()
        }
        //Viewの値をセット
        for i in 0..<self.currentItems.count {
            self.arcs[i].color1 = ConstValue.colorArcs[i % 10]
            self.arcs[i].color2 = ConstValue.colorArcDarks[i % 10]
            self.arcs[i].name = self.currentItems[i].name
        }
    }
    
    //animation
    private func actionTimeline() {
        withAnimation {
            if self.destroyFlag {
                return
            }
            self.drawRoulette()
            if self.actionState == 0 {
                self.actionAngle += Double(self.actionCount)
                self.actionCount += 1
                if self.actionCount > 50 {
                    self.actionAngle = self.actionAngle.truncatingRemainder(dividingBy: 360.0)
                    self.actionCount = 0
                    self.actionState = 1
                }
            } else if self.actionState == 1 {
                self.actionAngle += 89.7 + Double(self.actionCount / 10)
                self.actionCount += 1
                if self.actionCount > 50 {
                    self.actionAngle += Double.random(in: 0..<360.0)
                    self.actionAngle = self.actionAngle.truncatingRemainder(dividingBy: 360.0)
                    self.actionCount = 0
                    self.actionState = 2
                }
            } else if self.actionState == 2 {
                self.actionAngle += 100.0 - Double(self.actionCount / 2)
                self.actionCount += 1
                if self.actionCount > 200 {
                    self.actionAngle = self.actionAngle.truncatingRemainder(dividingBy: 360.0)
                    self.actionCount = 0
                    self.actionState = 3
                }
            } else if self.actionState == 3 {
                self.busyFlag = false
                return
            }
            DispatchQueue.global().async {
                Thread.sleep(forTimeInterval: 0.02)
                DispatchQueue.main.sync {
                    if self.destroyFlag == false {
                        self.actionTimeline()
                    }
                }
            }
        }
    }
    
    private func drawRoulette() {
        var startAngle: Double = self.actionAngle
        //判定結果計算
        var nowSelectItem: Int = -1
        for i in 0..<self.currentItems.count {
            let currentAngle: Double = self.currentItems[i].ratio / self.itemRateSum * 360.0   //書こうとする扇形の確度
            let startAngleTmp: Double = startAngle.truncatingRemainder(dividingBy: 360.0)
            if ((startAngleTmp <= 270.0) && (startAngleTmp + currentAngle > 270.0)) || ((startAngleTmp >= 270.0) && (startAngleTmp + currentAngle >= 630.0)) {
                nowSelectItem = i
                break
            }
            startAngle += currentAngle
        }
        //判定結果表示 back color
        if nowSelectItem != -1 {  //念のため
            self.colorBg = ConstValue.colorArcs[nowSelectItem % 10]
        }
        //円グラフ
        startAngle = self.actionAngle
        for i in 0..<self.currentItems.count {
            let currentAngle: Double = self.currentItems[i].ratio / self.itemRateSum * 360.0   //書こうとする扇形の確度
            //circle
            self.arcs[i].start = startAngle
            self.arcs[i].end = startAngle + currentAngle
            //
            startAngle += currentAngle
        }
        //判定結果表示 text
        if (nowSelectItem != -1) {  //念のため
            self.choiceResult = self.currentItems[nowSelectItem].name
        }
    }
    
    private func baseArc(size: CGFloat) -> some View {
        return Arc(startAngle: .degrees(-89), endAngle: .degrees(269))
            .fill(Color.white)
            .frame(width: size * 0.9, height: size * 0.9, alignment: .center)
            .animation(.none)
    }

    private func fillArc(start: Double, end: Double, color1: Color, color2: Color, size: CGFloat) -> some View {
        return ZStack {
            Arc(startAngle: .degrees(start), endAngle: .degrees(end))
                .fill(color1)
                .frame(width: size * 0.85, height: size * 0.85, alignment: .center)
                .animation(.none)
            Arc(startAngle: .degrees(start), endAngle: .degrees(end))
                .fill(color2)
                .frame(width: size * 0.5, height: size * 0.5, alignment: .center)
                .animation(.none)
        }
    }
    
    private func arcText(start: Double, end: Double, name: String, size: CGFloat) -> some View {
        let textAngle: CGFloat = CGFloat(((start + end) / 2.0)) + 90.0
        let textX: CGFloat = CGFloat(sin(textAngle * CGFloat.pi / 180) * (size / 3))
        let textY: CGFloat = CGFloat(cos(textAngle * CGFloat.pi / 180) * (size / 3) * -1)
        return ZStack {
            Text(name)
                .offset(x: textX, y: textY)
                .font(.footnote)
                .animation(.none)
        }
    }

}

struct Arc: Shape {
    var startAngle: Angle
    var endAngle: Angle
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.addLines([CGPoint(x:rect.midX, y:rect.midY)])
        path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: rect.width / 2, startAngle: startAngle, endAngle: endAngle, clockwise: false)
        //path.addLine(to: CGPoint(x:rect.midX, y:rect.midY))
        return path
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(PublicManager())
    }
}

PublicManager.swift

//
//  PublicManager.swift
//  Roulette
//
//  Created by akira ohmachi on 2021/04/03.
//

import SwiftUI
import GoogleMobileAds

class PublicManager: ObservableObject {
    
    @Published var isSetting: Bool = false
    
    @AppStorage("item1name") var item1name: String = ""
    @AppStorage("item2name") var item2name: String = ""
    @AppStorage("item3name") var item3name: String = ""
    @AppStorage("item4name") var item4name: String = ""
    @AppStorage("item5name") var item5name: String = ""
    @AppStorage("item6name") var item6name: String = ""
    @AppStorage("item7name") var item7name: String = ""
    @AppStorage("item8name") var item8name: String = ""
    @AppStorage("item9name") var item9name: String = ""
    @AppStorage("item10name") var item10name: String = ""
    @AppStorage("item11name") var item11name: String = ""
    @AppStorage("item12name") var item12name: String = ""
    @AppStorage("item13name") var item13name: String = ""
    @AppStorage("item14name") var item14name: String = ""
    @AppStorage("item15name") var item15name: String = ""
    @AppStorage("item16name") var item16name: String = ""
    @AppStorage("item17name") var item17name: String = ""
    @AppStorage("item18name") var item18name: String = ""
    @AppStorage("item19name") var item19name: String = ""
    @AppStorage("item20name") var item20name: String = ""

    @AppStorage("item1ratio") var item1ratio: Double = 0.0
    @AppStorage("item2ratio") var item2ratio: Double = 0.0
    @AppStorage("item3ratio") var item3ratio: Double = 0.0
    @AppStorage("item4ratio") var item4ratio: Double = 0.0
    @AppStorage("item5ratio") var item5ratio: Double = 0.0
    @AppStorage("item6ratio") var item6ratio: Double = 0.0
    @AppStorage("item7ratio") var item7ratio: Double = 0.0
    @AppStorage("item8ratio") var item8ratio: Double = 0.0
    @AppStorage("item9ratio") var item9ratio: Double = 0.0
    @AppStorage("item10ratio") var item10ratio: Double = 0.0
    @AppStorage("item11ratio") var item11ratio: Double = 0.0
    @AppStorage("item12ratio") var item12ratio: Double = 0.0
    @AppStorage("item13ratio") var item13ratio: Double = 0.0
    @AppStorage("item14ratio") var item14ratio: Double = 0.0
    @AppStorage("item15ratio") var item15ratio: Double = 0.0
    @AppStorage("item16ratio") var item16ratio: Double = 0.0
    @AppStorage("item17ratio") var item17ratio: Double = 0.0
    @AppStorage("item18ratio") var item18ratio: Double = 0.0
    @AppStorage("item19ratio") var item19ratio: Double = 0.0
    @AppStorage("item20ratio") var item20ratio: Double = 0.0

    @AppStorage("itemSplit") var itemSplit: Bool = false
    @AppStorage("itemReverse") var itemReverse: Bool = false
    
    var tmpItemSplit: Bool = false
    var tmpItemReverse: Bool = false

    struct Item {
        var name: String
        var ratio: Double
        init(name: String, ratio: Double) {
            self.name = name
            self.ratio = ratio
        }
    }
    
    var items: Array<Item> = [
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
        Item(name: "", ratio: 0.0),
    ]

    struct TmpItem {
        var name: String
        var ratio: String
        init(name: String, ratio: String) {
            self.name = name
            self.ratio = ratio
        }
    }

    var tmpItems: Array<TmpItem> = [
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
        TmpItem(name: "", ratio: ""),
    ]

    func saveItems() {
        self.item1name = self.items[0].name
        self.item1ratio = self.items[0].ratio
        self.item2name = self.items[1].name
        self.item2ratio = self.items[1].ratio
        self.item3name = self.items[2].name
        self.item3ratio = self.items[2].ratio
        self.item4name = self.items[3].name
        self.item4ratio = self.items[3].ratio
        self.item5name = self.items[4].name
        self.item5ratio = self.items[4].ratio
        self.item6name = self.items[5].name
        self.item6ratio = self.items[5].ratio
        self.item7name = self.items[6].name
        self.item7ratio = self.items[6].ratio
        self.item8name = self.items[7].name
        self.item8ratio = self.items[7].ratio
        self.item9name = self.items[8].name
        self.item9ratio = self.items[8].ratio
        self.item10name = self.items[9].name
        self.item10ratio = self.items[9].ratio
        self.item11name = self.items[10].name
        self.item11ratio = self.items[10].ratio
        self.item12name = self.items[11].name
        self.item12ratio = self.items[11].ratio
        self.item13name = self.items[12].name
        self.item13ratio = self.items[12].ratio
        self.item14name = self.items[13].name
        self.item14ratio = self.items[13].ratio
        self.item15name = self.items[14].name
        self.item15ratio = self.items[14].ratio
        self.item16name = self.items[15].name
        self.item16ratio = self.items[15].ratio
        self.item17name = self.items[16].name
        self.item17ratio = self.items[16].ratio
        self.item18name = self.items[17].name
        self.item18ratio = self.items[17].ratio
        self.item19name = self.items[18].name
        self.item19ratio = self.items[18].ratio
        self.item20name = self.items[19].name
        self.item20ratio = self.items[19].ratio
    }
    
    func loadItems() {
        self.items[0].name = self.item1name
        self.items[0].ratio = self.item1ratio
        self.items[1].name = self.item2name
        self.items[1].ratio = self.item2ratio
        self.items[2].name = self.item3name
        self.items[2].ratio = self.item3ratio
        self.items[3].name = self.item4name
        self.items[3].ratio = self.item4ratio
        self.items[4].name = self.item5name
        self.items[4].ratio = self.item5ratio
        self.items[5].name = self.item6name
        self.items[5].ratio = self.item6ratio
        self.items[6].name = self.item7name
        self.items[6].ratio = self.item7ratio
        self.items[7].name = self.item8name
        self.items[7].ratio = self.item8ratio
        self.items[8].name = self.item9name
        self.items[8].ratio = self.item9ratio
        self.items[9].name = self.item10name
        self.items[9].ratio = self.item10ratio
        self.items[10].name = self.item11name
        self.items[10].ratio = self.item11ratio
        self.items[11].name = self.item12name
        self.items[11].ratio = self.item12ratio
        self.items[12].name = self.item13name
        self.items[12].ratio = self.item13ratio
        self.items[13].name = self.item14name
        self.items[13].ratio = self.item14ratio
        self.items[14].name = self.item15name
        self.items[14].ratio = self.item15ratio
        self.items[15].name = self.item16name
        self.items[15].ratio = self.item16ratio
        self.items[16].name = self.item17name
        self.items[16].ratio = self.item17ratio
        self.items[17].name = self.item18name
        self.items[17].ratio = self.item18ratio
        self.items[18].name = self.item19name
        self.items[18].ratio = self.item19ratio
        self.items[19].name = self.item20name
        self.items[19].ratio = self.item20ratio

        var empty: Bool = true
        for i in 0..<items.count {
            if self.items[i].name != "" {
                empty = false
                break
            }
        }
        if empty {
            self.items[0].name = "first"
            self.items[0].ratio = 1.7
            self.items[1].name = "second"
            self.items[1].ratio = 1.5
            self.items[2].name = "third"
            self.items[2].ratio = 1.3
            self.items[3].name = "fourth"
            self.items[3].ratio = 1.1
            self.items[4].name = "fifth"
            self.items[4].ratio = 1.0
            self.saveItems()
        }
    }
    
    //--------------------------------------------------
    //adMob

    struct AdView: UIViewRepresentable {
        func makeUIView(context: Context) -> GADBannerView {
            let banner = GADBannerView(adSize: kGADAdSizeBanner)
            #if DEBUG
            banner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
            #else
            banner.adUnitID = "ca-app-pub-0000000000000000/0000000000"
            #endif
            banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
            banner.load(GADRequest())
            return banner
        }

        func updateUIView(_ uiView: GADBannerView, context: Context) {
        }
    }

    //--------------------------------------------------

}

RouletteApp.swift

//
//  RouletteApp.swift
//  Roulette
//
//  Created by akira ohmachi on 2021/04/03.
//

import SwiftUI
import UIKit
import GoogleMobileAds

// AppDelegateクラスを定義する
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

        // Mobile Ads SDKを初期化する
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        
        return true
    }
}

@main
struct RouletteApp: App {
    // SwiftUI AppライフサイクルにAppDelegateクラスを注入する
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(PublicManager())
        }
    }
}

SettingView.swift

//
//  SettingView.swift
//  Roulette
//
//  Created by akira ohmachi on 2021/04/03.
//

import SwiftUI

struct SettingView: View {
    @EnvironmentObject var pub: PublicManager
    
    let inputNameWidth: CGFloat = 0.55

    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    pub.isSetting = false
                }) {
                    HStack {
                        Image(systemName: "arrow.backward")
                        Text("cancel")
                    }
                }
                Spacer()
                Button(action: {
                    for i in 0..<self.pub.items.count {
                        self.pub.items[i].name = self.pub.tmpItems[i].name
                        self.pub.items[i].ratio = self.adjustStrToNum(str: self.pub.tmpItems[i].ratio)
                    }
                    self.pub.saveItems()
                    self.pub.loadItems()
                    self.pub.itemSplit = self.pub.tmpItemSplit
                    self.pub.itemReverse = self.pub.tmpItemReverse
                    pub.isSetting = false
                }) {
                    HStack {
                        Text("apply")
                        Image(systemName: "checkmark.circle")
                    }
                }
            }
            .padding(20)
        }
        GeometryReader { bodyView in
            Form {
                Section(header: Text("name / ratio")) {
                    VStack {
                        ForEach(0..<self.pub.items.count) { num in
                            HStack {
                                Text("\(String(format: "%02d",num + 1))").font(.caption2)
                                TextField("", text: self.$pub.tmpItems[num].name)
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(minWidth: bodyView.size.width * self.inputNameWidth)
                                TextField("", text: Binding(
                                            get: {self.pub.tmpItems[num].ratio},
                                            set: {self.pub.tmpItems[num].ratio = $0.filter{".0123456789".contains($0)}}))
                                            .textFieldStyle(RoundedBorderTextFieldStyle())
                                            .keyboardType(.decimalPad)
                            }
                        }
                    }
                }
                Section(header: Text("split")) {
                    Toggle(isOn: self.$pub.tmpItemSplit) {
                        Text("splitItem")
                            .font(.subheadline)
                    }
                    Text("splitItemNote")
                        .font(.footnote)
                }
                Section(header: Text("reverse")) {
                    Toggle(isOn: self.$pub.tmpItemReverse) {
                        Text("reverseItem")
                            .font(.subheadline)
                    }
                    Text("reverseItemNote")
                        .font(.footnote)
                }
                Section(header: Text("usage")) {
                    Text("usage1")
                        .font(.footnote)
                }
            }
        }
        //.navigationBarTitle("Setting",displayMode: .automatic)
        //.navigationViewStyle(StackNavigationViewStyle())
        .navigationBarHidden(true)
        .onTapGesture {     //背景タップでキーボードを閉じる
            UIApplication.shared.closeKeyboard()
        }
    }
    
    private func adjustStrToNum(str: String) -> Double {
        let ary: Array<String> = (str + ".0").components(separatedBy: ".")
        let num: Double = NumberFormatter().number(from: "0" + ary[0] + "." + ary[1]) as! Double
        return num
    }
}

extension UIApplication {
    func closeKeyboard() {  //キーボードを閉じる
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

struct SettingView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            SettingView().environmentObject(PublicManager())
        }
    }
}
このページでお気付きの点がございましたら
ご連絡いただけますと幸いです。
Eメール(任意)
送信内容
 

■ 便利ウェブサイト