带定时器的按钮.当在定时器结束之前按下按钮不止一次时,间隔是永无止境的



我一直在开发这个Truth、Dare或Drink应用程序,我有一个button,当按下它时,它会旋转卡片3秒,然后随机选择一张Truth、Dare或Drinks卡片,并随机选择一个Truth或Dare。

我现在唯一的问题是,如果你在timer完成之前多次按下button,它就会一直持续下去,永远不会停止。

我正在想办法在timer完成之前制作buttondisabled,但我什么都没想出来。有人知道我该怎么解决这个问题吗?我不知所措。

import SwiftUI
import CoreHaptics

class Colors{
static var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard"), Color("drinkcard2"), Color("drinkcard")]), startPoint: .top, endPoint: .bottom)]
static var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard"), Color("truthcard")]), startPoint: .top, endPoint: .bottom)]
static var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard"), Color("darecard")]), startPoint: .top, endPoint: .bottom)]
}
class TruthorDares{
static var truths = [String("Have you ever walked in on your parents doing it?"), String("Have you ever tasted a booger?"), String("Have you ever farted in an elevator?"), String("Have you ever peed yourself?"), String("What would be in your web history that you’d be embarrassed if someone saw?"),
String("Who is the sexiest person in this room?"), String("What was the last thing you texted?"), String("What is the most illegal thing you have ever done?"),
String("Would you rather lose your sex organs forever or gain 200 pounds?"), String("If someone offered you $1 million to break up with your girlfriend/boyfriend, would you do it?")]
static var dares = [String("Let someone put lipstick on your lips."), String("Drink a glass of wine in less than 15 seconds."), String("Fill your mouth with water and try singing a song."), String("Striptease for thirty whole seconds."), String("Remove the socks of a person sitting next to you with your teeth."), String("Take off one item of clothing."), String("Wet your hair and apply shampoo, but don’t rinse it off."), String("Most the most embarrassing photo of yours, as FB profile picture."), String("Let someone sitting next to you spank you."), String("Call a random number and talk dirty to them.")]
static var placeHolders = [String("")]
static var drink = [String("Take a shot, or chug the rest of your drink!!")]
}
struct ContentView: View {
// Picks foreground color based on which card is picked
@State private var foregroundPicker = [1]
// Picks which placeholder is going to be chosen based on which card is picked
@State private var randText = [0]
//Picks the random truth, dare or drink string from the TruthorDares class depending on which card is chosen
@State private var placeHolder = [String("")]
@State private var timer:Timer!
//Picks which text to be shown on card
@State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
@State private var foregrounds = [Color.white, Color.black]
//Picks which text and card color are chosen
@State private var number = [0]
//Keeps time for the time interval in the spin button
@State private var timeKeeper: Float = 0
//For keeping the card spinning
@State private var angle: Double = 0
let generator = UINotificationFeedbackGenerator()
//For choosing the color of the card depending on which text is chosen
@State var backgrounds = [Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.drinkGradient[0]]


var body: some View {
ZStack{
//Background
Rectangle()
.foregroundColor(Color("background"))
.edgesIgnoringSafeArea(.all)
VStack{
HStack {
//Top banner
Text("Truth, Dare or Drink")
.shadow(radius: 10)
.padding(.top, 20)
.foregroundColor(.white)
.font(.system(.largeTitle, design: .rounded) )
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100)
.background(Color("banner"))
.edgesIgnoringSafeArea(.top)
.shadow(radius: 15)
}
//Where the truth or dare will go depending on which one is picked
Text(String(placeHolder[0]))
.frame(width: 350, height: 200)
.foregroundColor(.white)
.font(.system(.headline, design: .rounded))
Spacer()
//The truth, dare or drink random card
Text(text[number[0]])
.font(.system(.largeTitle, design: .rounded))
.foregroundColor(foregrounds[foregroundPicker[0]])
.frame(width: 250, height: 250)
.background(backgrounds[number[0]])
.clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
.shadow(radius: 10)
.rotationEffect(.degrees(angle))
.animation(.easeIn)
Spacer()
Button(action: {
self.timer = Timer.scheduledTimer(withTimeInterval: 0.10, repeats: true, block: {_ in
self.timeKeeper += 0.10
let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
impactHeavy.impactOccurred()
if self.timeKeeper < 3{
if self.number[0] == self.text.count - 1{
self.number[0] = 0
self.number[0] += 1
self.angle += 360
}
else{
self.number[0] += 1
self.angle += 360
}
}
else{
self.number[0] = Int.random(in:
0...self.text.count - 1)
self.angle += 360
self.timeKeeper = 0
if self.number[0] == 0 || self.number[0] == 2 || self.number[0] == 4{
self.placeHolder[0] = TruthorDares.truths.randomElement()!
self.foregroundPicker[0] = Int(1)
}
else if self.number[0] == 1 || self.number[0] == 3 || self.number[0] == 5{
self.placeHolder[0] = TruthorDares.dares.randomElement()!
self.foregroundPicker[0] = Int(0)
}
else{
self.placeHolder[0] = TruthorDares.drink.randomElement()!
self.foregroundPicker[0] = Int(1)
}
self.timer.invalidate()
}
})
}) {
Text("Spin")
.font(.system(.title, design: .rounded))
.foregroundColor(.white)
.frame(width: 250, height: 50)
.background(Color("button"))
.clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
.shadow(radius: 15)
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

您可以简单地执行if语句,检查计时器是否完成,如果完成,请重新启用按钮。如果计时器重新启动,则按钮是可行的。有道理吗?

禁用Swift do:buttonName.isEnabled = false中的按钮

然后要重新启用它,只需将false切换为true

我只需要:

@State private var isBusy = false

并在按钮中使用:

Button(action: {
if !self.isBusy {
self.isBusy = true
// ... your code here
}

就在定时器完成之前:

self.isBusy = false

最新更新