ConstValue.swift
//
// ConstValue.swift
// FortuneSlip
//
// Created by akira ohmachi on 2021/04/06.
//
import SwiftUI
class ConstValue: ObservableObject {
//background
static let colorBg: Color = Color.init(red: 254 / 255, green: 0, blue: 0)
//button color
static let colorStart: Color = Color.init(red: 254 / 255, green: 193 / 255, blue: 7 / 255)
static let colorSetting: Color = Color.init(red: 255 / 256, green: 152 / 255, blue: 1 / 255)
}
ContentView.swift
//
// ContentView.swift
// FortuneSlip
//
// Created by akira ohmachi on 2021/04/06.
//
import SwiftUI
import GoogleMobileAds
struct ContentView : View {
@EnvironmentObject var pub: PublicManager
@State private var busyFlag: Bool = false
@State private var destroyFlag: Bool = false
@State private var omikujiImageName: String = "omikuji001"
@State private var timeLine: Int = 0
@State private var resultTextName: String = ""
@State private var resultTextAlpha: Double = 0.0
@State private var resultTextPosition: CGPoint = CGPoint(x: 0, y: 0)
struct ResultTextPosition {
let time: Int
let x: Int
let y: Int
let alpha: Double
init(time: Int, x: Int, y: Int, alpha: Double) {
self.time = time
self.x = x
self.y = y
self.alpha = alpha
}
}
private var resultTextPositions: Array<ResultTextPosition> = [
ResultTextPosition(time: 84, x: 547, y: 440, alpha: 0.00),
ResultTextPosition(time: 85, x: 549, y: 444, alpha: 0.03),
ResultTextPosition(time: 86, x: 550, y: 450, alpha: 0.06),
ResultTextPosition(time: 87, x: 550, y: 454, alpha: 0.09),
ResultTextPosition(time: 88, x: 552, y: 459, alpha: 0.11),
ResultTextPosition(time: 89, x: 554, y: 464, alpha: 0.14),
ResultTextPosition(time: 90, x: 555, y: 469, alpha: 0.17),
ResultTextPosition(time: 91, x: 556, y: 474, alpha: 0.20),
ResultTextPosition(time: 92, x: 558, y: 479, alpha: 0.23),
ResultTextPosition(time: 93, x: 560, y: 484, alpha: 0.26),
ResultTextPosition(time: 94, x: 562, y: 489, alpha: 0.29),
ResultTextPosition(time: 95, x: 563, y: 495, alpha: 0.31),
ResultTextPosition(time: 96, x: 565, y: 500, alpha: 0.34),
ResultTextPosition(time: 97, x: 567, y: 505, alpha: 0.37),
ResultTextPosition(time: 98, x: 569, y: 511, alpha: 0.40),
ResultTextPosition(time: 99, x: 571, y: 516, alpha: 0.43),
ResultTextPosition(time: 100, x: 573, y: 522, alpha: 0.46),
ResultTextPosition(time: 101, x: 575, y: 530, alpha: 0.49),
ResultTextPosition(time: 102, x: 578, y: 539, alpha: 0.51),
ResultTextPosition(time: 103, x: 580, y: 550, alpha: 0.54),
ResultTextPosition(time: 104, x: 583, y: 563, alpha: 0.57),
ResultTextPosition(time: 105, x: 587, y: 576, alpha: 0.60),
ResultTextPosition(time: 106, x: 590, y: 592, alpha: 0.63),
ResultTextPosition(time: 107, x: 594, y: 608, alpha: 0.66),
ResultTextPosition(time: 108, x: 599, y: 626, alpha: 0.69),
ResultTextPosition(time: 109, x: 604, y: 645, alpha: 0.71),
ResultTextPosition(time: 110, x: 608, y: 665, alpha: 0.74),
ResultTextPosition(time: 111, x: 614, y: 687, alpha: 0.77),
ResultTextPosition(time: 112, x: 619, y: 708, alpha: 0.80),
ResultTextPosition(time: 113, x: 625, y: 730, alpha: 0.83),
ResultTextPosition(time: 114, x: 630, y: 751, alpha: 0.86),
ResultTextPosition(time: 115, x: 636, y: 772, alpha: 0.89),
ResultTextPosition(time: 116, x: 641, y: 792, alpha: 0.91),
ResultTextPosition(time: 117, x: 646, y: 808, alpha: 0.94),
ResultTextPosition(time: 118, x: 651, y: 822, alpha: 0.97),
ResultTextPosition(time: 119, x: 654, y: 832, alpha: 1.00),
]
var body: some View {
NavigationView{
GeometryReader { bodyView in
VStack(spacing: 0) {
HStack(spacing: 0) {
Button(action:{
self.omikujiStart()
}){
Text("start")
.frame(width: bodyView.size.width / 4 * 3, height: bodyView.size.height / 6, alignment: .center)
.background(ConstValue.colorStart)
.foregroundColor(.primary)
}
Rectangle().fill(Color.white).frame(width: 1,height: bodyView.size.height / 6)
NavigationLink(destination: SettingView(),isActive: self.$pub.isSetting) {
Button(action: {
self.buttonActionSetting()
}){
Text("setting")
.frame(width: bodyView.size.width / 4, height: bodyView.size.height / 6, alignment: .center)
.background(ConstValue.colorSetting)
.foregroundColor(.primary)
}
}
.background(ConstValue.colorBg)
}
.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: 20)
ZStack {
Image(self.omikujiImageName)
.resizable()
.frame(width: bodyView.size.width, height: bodyView.size.width, alignment: .center)
self.omikujiText(size: bodyView.size.width)
}
}
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(ConstValue.colorBg)
.navigationBarHidden(true)
}
.navigationViewStyle(StackNavigationViewStyle())
.onDisappear { //アプリ終了時
self.destroyFlag = true
}
}
private func omikujiText(size: CGFloat) -> some View {
let offsetX: CGFloat = size / 900.0 * self.resultTextPosition.x - (size / 2) - 20
let offsetY: CGFloat = size / 900.0 * self.resultTextPosition.y - (size / 2) - 20
var body : some View {
Text(self.resultTextName)
.foregroundColor(Color.white)
.font(.title)
.opacity(self.resultTextAlpha)
.offset(CGSize(width: offsetX, height: offsetY))
.animation(.none)
}
return body
}
private func initLotteryItems() {
if self.pub.lotteryItem1 == ""
&& self.pub.lotteryItem2 == ""
&& self.pub.lotteryItem3 == ""
&& self.pub.lotteryItem4 == ""
&& self.pub.lotteryItem5 == ""
&& self.pub.lotteryItem6 == ""
&& self.pub.lotteryItem7 == ""
{
self.pub.qtyItem1 = "1"
self.pub.qtyItem2 = "3"
self.pub.qtyItem3 = "5"
self.pub.qtyItem4 = "5"
self.pub.qtyItem5 = "5"
self.pub.qtyItem6 = "1"
self.pub.qtyItem7 = "1"
self.pub.lotteryItem1 = NSLocalizedString("rank1" , comment : "rank1")
self.pub.lotteryItem2 = NSLocalizedString("rank2" , comment : "rank2")
self.pub.lotteryItem3 = NSLocalizedString("rank3" , comment : "rank3")
self.pub.lotteryItem4 = NSLocalizedString("rank4" , comment : "rank4")
self.pub.lotteryItem5 = NSLocalizedString("rank5" , comment : "rank5")
self.pub.lotteryItem6 = NSLocalizedString("rank6" , comment : "rank6")
self.pub.lotteryItem7 = NSLocalizedString("rank7" , comment : "rank7")
}
}
private func buttonActionSetting() {
if self.busyFlag {
return
}
self.initLotteryItems()
self.pub.tmpLotteryItem1 = self.pub.lotteryItem1
self.pub.tmpLotteryItem2 = self.pub.lotteryItem2
self.pub.tmpLotteryItem3 = self.pub.lotteryItem3
self.pub.tmpLotteryItem4 = self.pub.lotteryItem4
self.pub.tmpLotteryItem5 = self.pub.lotteryItem5
self.pub.tmpLotteryItem6 = self.pub.lotteryItem6
self.pub.tmpLotteryItem7 = self.pub.lotteryItem7
self.pub.tmpQtyItem1 = self.pub.qtyItem1
self.pub.tmpQtyItem2 = self.pub.qtyItem2
self.pub.tmpQtyItem3 = self.pub.qtyItem3
self.pub.tmpQtyItem4 = self.pub.qtyItem4
self.pub.tmpQtyItem5 = self.pub.qtyItem5
self.pub.tmpQtyItem6 = self.pub.qtyItem6
self.pub.tmpQtyItem7 = self.pub.qtyItem7
self.pub.isSetting = true
}
private func setImageName() {
self.omikujiImageName = "omikuji" + String(format: "%03d", self.timeLine)
}
private func omikujiMove() {
if self.timeLine < 84 || self.timeLine > 119 {
return
}
for i in 0..<self.resultTextPositions.count {
if self.resultTextPositions[i].time == self.timeLine {
self.resultTextPosition = CGPoint(x: self.resultTextPositions[i].x, y: self.resultTextPositions[i].y)
self.resultTextAlpha = self.resultTextPositions[i].alpha
break
}
}
}
private func omikujiStart() {
if self.busyFlag {
return
}
self.busyFlag = true
self.initLotteryItems()
self.resultTextName = ""
self.resultTextAlpha = 0.0
let qtys: Array<Int> = [
NumberFormatter().number(from: "0" + self.pub.qtyItem1) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem2) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem3) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem4) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem5) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem6) as! Int,
NumberFormatter().number(from: "0" + self.pub.qtyItem7) as! Int,
]
var omikujiSum: Int = 0
for i in 0 ..< qtys.count {
omikujiSum += qtys[i]
}
var remainNum: Int = Int.random(in: 0 ..< omikujiSum)
var choiceLottery: Int = -1
for i in 0 ..< qtys.count {
if qtys[i] != 0 {
if qtys[i] <= remainNum {
remainNum -= qtys[i]
} else {
choiceLottery = i
break
}
}
}
switch choiceLottery {
case 0: self.resultTextName = self.pub.lotteryItem1
case 1: self.resultTextName = self.pub.lotteryItem2
case 2: self.resultTextName = self.pub.lotteryItem3
case 3: self.resultTextName = self.pub.lotteryItem4
case 4: self.resultTextName = self.pub.lotteryItem5
case 5: self.resultTextName = self.pub.lotteryItem6
case 6: self.resultTextName = self.pub.lotteryItem7
default: break
}
self.timeLine = 0
self.timeLineAnimation()
}
private func timeLineAnimation() {
withAnimation {
if self.destroyFlag {
return
}
self.timeLine += 1
if self.timeLine > 120 {
self.busyFlag = false
return
}
self.setImageName()
self.omikujiMove()
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 0.02)
DispatchQueue.main.sync {
if self.destroyFlag == false {
self.timeLineAnimation()
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView().environmentObject(PublicManager())
}
}
}