有没有一种方法可以自动使用动画更改文本值?

发布于 2025-02-09 07:16:36 字数 1320 浏览 1 评论 0原文

我有一个加载屏幕,我想在其中显示一个通过动画自动更改其值的文本。

I have my logo rotating indefinitely without any button action

//logo
                    Image("reny")
                        .rotationEffect(.degrees(rotateDegree))
                        .onAppear(perform: {
                                 withAnimation(Animation.linear(duration: 4).repeatForever(autoreverses: false)) {
                                     self.rotateDegree = 360
                                 }
                             })

I assumed it was possible to do the same for a text using a string array but it doesn't work

@State var texts = ["Find the Apartment you like", "send an application", "we'll approve you in secs baby!"]
    @State var textIndex : Int = 0
//introduction text
                    Text(texts[textIndex]).bold()
                        .font(.title)
                        .onAppear(perform: {
                            withAnimation(Animation.linear(duration: 2).repeatForever(autoreverses: false)) {
                                textIndex += 1
                            }
                             })

does anybody know how to change the value of a text with an animation automatically?

我的目的是展示如何在此加载时间内使用该应用程序。

I have a loading screen where I want to show a text changing its value automatically with an animation.

I have my logo rotating indefinitely without any button action

//logo
                    Image("reny")
                        .rotationEffect(.degrees(rotateDegree))
                        .onAppear(perform: {
                                 withAnimation(Animation.linear(duration: 4).repeatForever(autoreverses: false)) {
                                     self.rotateDegree = 360
                                 }
                             })

I assumed it was possible to do the same for a text using a string array but it doesn't work

@State var texts = ["Find the Apartment you like", "send an application", "we'll approve you in secs baby!"]
    @State var textIndex : Int = 0
//introduction text
                    Text(texts[textIndex]).bold()
                        .font(.title)
                        .onAppear(perform: {
                            withAnimation(Animation.linear(duration: 2).repeatForever(autoreverses: false)) {
                                textIndex += 1
                            }
                             })

does anybody know how to change the value of a text with an animation automatically?

my intention is to show how to use the app during this loading time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2025-02-16 07:16:36

这是一种可能的方法 - 仅根据索引替换文本,并在出现后不断更改索引。

Tested with Xcode 13.4 / iOS 15.5

demo

Main part:

private func next() {
    var next = textIndex + 1
    if next == texts.count {
        next = 0
    }
    withAnimation(Animation.linear(duration: 2)) {
        textIndex = next
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.next()
    }
}

and text itself

Text(texts[textIndex]).bold()
    .font(.title).id(textIndex)
    .onAppear(perform: {
        next()
    })

Here is a possible approach - just to replace Text depending on index and continuously change index after appear.

Tested with Xcode 13.4 / iOS 15.5

demo

Main part:

private func next() {
    var next = textIndex + 1
    if next == texts.count {
        next = 0
    }
    withAnimation(Animation.linear(duration: 2)) {
        textIndex = next
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.next()
    }
}

and text itself

Text(texts[textIndex]).bold()
    .font(.title).id(textIndex)
    .onAppear(perform: {
        next()
    })

Test module on GitHub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文