123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- //
- // GetCheckCodeButton.swift
- // fiveConstant
- //
- // Created by 李建 on 2023/1/14.
- //
- import SwiftUI
- import Combine
- struct GetCheckCodeButton: View {
- @State var countSecond = 60
- @State var isCountStart: Bool = false
- @State var timer: Timer?
- var act: ()->()?
- init(action: @escaping ()->Void){
- self.act = action
- }
- var body: some View {
- Button(action: {
- self.startTimer()
- self.act()
- }, label: {
- Text(!isCountStart ? "获取验证码" : "\(countSecond)S")
- .font(.caption)
- .foregroundColor(isCountStart ? Color.gray : Color("MainColor"))
- .padding(7)
- .frame(width: 80, height: 30)
- .overlay(RoundedRectangle(cornerRadius: 50)
- .stroke(isCountStart ? Color.gray : Color("MainColor"))
- )
- }).disabled(isCountStart)
- }
-
- func startTimer() {
- isCountStart = true
- timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
- if countSecond == 0 {
- isCountStart = false
- countSecond = 60
- timer?.invalidate()
- timer = nil
- }
- countSecond -= 1
- })
- }
- }
- struct GetCheckCodeButton_Previews: PreviewProvider {
- static var previews: some View {
- GetCheckCodeButton {
- print("1`23")
- }
- }
- }
|