ios、swift、swfitUI发送验证码倒计时onReceive接收自定义事件
SwiftUI通过.onReceive使用Combine的方式,它需要一个发布者。您可以将可观察对象作为属性公开发布者我们的timer正好是一个发布者struct VerificationPage: View {@State var countDown:Int = 60var body: some View {Button(action: {self.countDown = 60
·
SwiftUI通过.onReceive使用Combine的方式,它需要一个发布者。您可以将可观察对象作为属性公开发布者
我们的timer正好是一个发布者
struct VerificationPage: View {
@State var countDown:Int = 60
var body: some View {
Button(action: {
self.countDown = 60
}, label: {
if self.countDown != 0{
Text("\(self.countDown)s")
.foregroundColor(.white)
//这里直接将Timer作为发送者every是每隔一秒
.onReceive(Timer.publish(every: 1, on: .main, in: .common).autoconnect(), perform: { _ in
//不等于零每隔一秒减一
if (self.countDown != 0) {
self.countDown -= 1
}
})
}else{
Text("获取验证码")
.font(.system(size: 14))
.foregroundColor(.white)
}
})
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)