gpt4 book ai didi

ios - 变量不在范围内,即使它出现在 Swift 中同一范围内的其他地方

转载 作者:行者123 更新时间:2023-12-04 07:40:37 24 4
gpt4 key购买 nike

我正在 Swift 中测试 Prafulla Singh 的字幕文本,我目前遇到一个错误,即变量声称不在范围内,即使在其他地方看起来是相同的范围内,它也被调用和工作。我附上了整个代码,因为我觉得好像有必要查看变量范围。

struct test_marquee: App {
var body: some Scene {
WindowGroup {
struct Marque: View {
let text: String
@State private var moveView = false
@State private var stopAnimation = false
// ####
@State private var textFrame: CGRect = CGRect() //DECLARATION FOR ERROR VARIABLE
public init(text: String) {
self.text = text
}
var body: some View {
GeometryReader { proxy in
ScrollView(.horizontal, showsIndicators: false, content: {
Text(text)
.lineLimit(1)
.background(GeometryGetter(rect: $textFrame)).offset(moveView ? CGSize(width: -1 * textFrame.width, height: 0) : CGSize(width: proxy.size.width, height: 0))
.onAppear() {
self.stopAnimation = false
animateView()
moveViewOnAnimationEnd()///scrollViewProxy.scrollTo("Identifier") /// does not animate
}.onDisappear() {
self.stopAnimation = true
}
}).mask(LinearGradient(gradient: Gradient(colors: [Color.clear, Color.black, Color.black, Color.clear]), startPoint: .leading, endPoint: .trailing))
.padding([.top, .bottom], 100)
}
}
private func animateView() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1, execute: { //after 0.5 sec
withAnimation(Animation.linear(duration: Double(textFrame.width) * 0.01)) {
// ######
//textFrame IS CALLED HERE WITH NO ISSUES
moveView = true
}//no on completion so need to add another time bound method to restart animation from start
})
}
private func moveViewOnAnimationEnd() {
// ######
let timeToAnimate = (Double(textFrame.width) * 0.01) + 0.2
// ERROR OCCURS ON THIS LINE ON THE textFrame rect
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + timeToAnimate, execute: { //after 0.5 sec
moveView = false
if stopAnimation == false {
animateView()
moveViewOnAnimationEnd()
}
})
}


struct GeometryGetter: View {
@Binding var rect: CGRect

var body: some View {
GeometryReader { (proxy) -> Path in
DispatchQueue.main.async {
self.rect = proxy.frame(in: .global)
}
return Path()
}
}
}

struct Marque_Previews: PreviewProvider {

static var previews: some View {
Marque(text: "If you don't provide your own init with an explicit public modifier the generated constructor will be marked internal")
}
}
}
}
}
}

最佳答案

这可能是 ViewBuilder(或 _functionBuilder)的错误,这就是 WindowGroup正在使用它的内容...但是你为什么要声明 Marque类型 里面 WindowGroup { }内容关闭?
正常声明,在 WindowGroup 之外的闭包,但在外部结构内部,如果您愿意:

struct TestMarquee: App {
struct Marque: View {
// the rest of your code...
}

var body: some Scene {
WindowGroup {
Marque(text: "whatever") // instantiate here; don't declare
}
}
}
{ }WindowGroup表示尾随闭包语法。但这是 @ViewBuilder 使用的“特殊”类型的闭包。提取( read here for more info) View s 来自闭包内的每个代码块。这些不是 { }用于定义类似结构或函数的范围。

关于ios - 变量不在范围内,即使它出现在 Swift 中同一范围内的其他地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67496576/

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