gpt4 book ai didi

ios - 我怎样才能简单地将 View 转换为 SwiftUI View ?

转载 作者:行者123 更新时间:2023-11-29 05:13:12 27 4
gpt4 key购买 nike

我想通过 SwiftUI 和 Timer 实现一个简单的 View 转换。我有一个主要 View ,它是内容 View 。如果我从 View 中调用 func FireTimer() ,该函数将触发计时器。然后 5 秒后,我将进行 View 转换。

我尝试了 NavigationLink,但它有一个按钮。计时器无法按下按钮,所以现在我很困惑。我将在下面显示我的代码。

TimerFire.swift

import Foundation
import UIKit
import SwiftUI

let TIME_MOVENEXT = 5
var timerCount : Int = 0

class TimerFire : ObservableObject{
var workingTimer = Timer()

@objc func FireTimer() {
print("FireTimer")
workingTimer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(TimerFire.timerUpdate),
userInfo: nil,
repeats: true)
}

@objc func timerUpdate(timeCount: Int) {
timerCount += 1
let timerText = "timerCount:\(timerCount)"
print(timerText)

if timerCount == TIME_MOVENEXT {
print("timerCount == TIME_MOVENEXT")

workingTimer.invalidate()
print("workingTimer.invalidate()")
timerCount = 0

//
//want to have a transition to SecondView here
//
}
}
}

ContentView.swift

import SwiftUI

struct ContentView: View {
var body: some View {
Button(action: {
// What to perform
let timerFire = TimerFire()
timerFire.FireTimer()
}) {
// How the button looks like
Text("Fire timer")
}
}
}

SecondView.swift

import Foundation
import SwiftUI

struct SecondView: View {
var body: some View {
Text("Second World")
}
}

我怎样才能简单地显示这个SecondView?

最佳答案

好的,如果您想在第一个屏幕上不使用 NavigationView (出于任何原因)执行此操作,这里有一种基于两个 View 之间转换的可能方法。

注意:预览版对过渡的支持有限,因此请在模拟器和真实设备上进行测试

这是一个演示,它的外观(初始白屏只是模拟器启动)

SwiftUI transition on timer

这是单个测试模块:

import SwiftUI
import UIKit

let TIME_MOVENEXT = 5
var timerCount : Int = 0

class TimerFire : ObservableObject{
var workingTimer = Timer()
@Published var completed = false

@objc func FireTimer() {
print("FireTimer")
workingTimer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(TimerFire.timerUpdate),
userInfo: nil,
repeats: true)
}

@objc func timerUpdate(timeCount: Int) {
timerCount += 1
let timerText = "timerCount:\(timerCount)"
print(timerText)

if timerCount == TIME_MOVENEXT {
print("timerCount == TIME_MOVENEXT")

workingTimer.invalidate()
print("workingTimer.invalidate()")
timerCount = 0

//
//want to have a transition to SecondView here
//
self.completed = true
}
}
}

struct SecondView: View {
var body: some View {
Text("Second World")
}
}

struct TestTransitionByTimer: View {
@ObservedObject var timer = TimerFire()

@State var showDefault = true
var body: some View {
ZStack {
Rectangle().fill(Color.clear) // << to make ZStack full-screen
if showDefault {
Rectangle().fill(Color.blue) // << just for demo
.overlay(Text("Hello, World!"))
.transition(.move(edge: .leading))
}
if !showDefault {
Rectangle().fill(Color.red) // << just for demo
.overlay(SecondView())
.transition(.move(edge: .trailing))
}
}
.onAppear {
self.timer.FireTimer()
}
.onReceive(timer.$completed, perform: { completed in
withAnimation {
self.showDefault = !completed
}
})
}
}

struct TestTransitionByTimer_Previews: PreviewProvider {
static var previews: some View {
TestTransitionByTimer()
}
}

关于ios - 我怎样才能简单地将 View 转换为 SwiftUI View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59483107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com