简体   繁体   English

SwiftUI,如何使背景颜色动画多次

[英]SwiftUI, how to make the background color animate multiple times

I have a SwiftUI project that includes a Text object with a.onTapGesture working that, when triggered, should cause the button's background color to pop to another color then quickly fade back to the original color.我有一个 SwiftUI 项目,其中包括一个带有 a.onTapGesture 的文本 object 工作,当触发时,应该会导致按钮的背景颜色弹出到另一种颜色,然后迅速淡化回原始颜色。 In my code, I'm able to trigger the color pop, but it stays that way and won't fade back.在我的代码中,我可以触发颜色弹出,但它会保持这种状态并且不会褪色。 I'm kind of at a loss on what to do, any help is appreciated...here is the code I'm using:我有点不知所措,感谢任何帮助......这是我正在使用的代码:

@State var buttonFlash = false


var body: some View {
    Text("Hello")
        .font(.system(size: 20))
        .frame(width: 30, height: 30)
        .foregroundColor(Color.blue)
        .background(buttonFlash ? Color.white : Color.red)
        .animation(nil)
        .background(Color.red)
        .animation((Animation.linear).delay(0.1))
        .cornerRadius(30)
        .onTapGesture {
            print("Tapped!")
            self.buttonFlash.toggle()

        }

should cause the button's background color to pop to another color then quickly fade back to the original color.应该会导致按钮的背景颜色弹出到另一种颜色,然后快速淡化回原始颜色。

You need to declare a function for that.您需要为此声明一个 function。 Here is a possible approach for it:这是一种可能的方法:

struct ContentView: View {
    
    @State var buttonFlash: Bool = false
    
    var body: some View {
        Text("Hello")
            .font(.system(size: 20))
            .frame(width: 100, height: 50)
            .foregroundColor(Color.blue)
            .background(buttonFlash ? Color.white : Color.red)
            .cornerRadius(30)
            .onTapGesture {
                print("Tapped!")
                self.colorChange()
            }
    }
    
    private func colorChange() {
        // first toggle makes it red
        self.buttonFlash.toggle()
        
        // wait for 1 second
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
            // Back to normal with ease animation
            withAnimation(.easeIn){
                self.buttonFlash.toggle()
            }
        })
    }
}

It calls the method once tapped.一旦被点击,它就会调用该方法。 This then triggers the color change and reverses it after 1 second.然后这会触发颜色变化并在 1 秒后将其反转。

Produces this outcome:产生这个结果:

在此处输入图像描述

If you're trying to flash the backgroundColor multiple number of times.如果您尝试 flash 多次使用backgroundColor Use a method and loop over until a condition is met.使用方法并循环直到满足条件。 Here's an example:这是一个例子:

struct ContentView: View {

    @State private var buttonFlash: Bool = false
    @State private var numberOfTimes = 6

    var body: some View {
        Text("Hello")
            .font(.system(size: 20))
            .frame(width: 300, height: 50)
            .foregroundColor(Color.blue)
            .background(buttonFlash ? Color.white : Color.red)
            .animation(nil)
            .background(Color.red)
            .animation((Animation.linear).delay(0.1))
            .cornerRadius(30)
            .onTapGesture {
                print("Tapped!")
                self.animate()
            }
    }

    private func animate() {
        self.buttonFlash.toggle()
        if numberOfTimes > 0 {
            numberOfTimes -= 1
        } else {
            numberOfTimes = 6
            return
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
            self.animate()
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM