Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swiftUI创建平滑的颜色变化动画?(有关例子)_Swift_Animation_Button_Swiftui - Fatal编程技术网

如何使用swiftUI创建平滑的颜色变化动画?(有关例子)

如何使用swiftUI创建平滑的颜色变化动画?(有关例子),swift,animation,button,swiftui,Swift,Animation,Button,Swiftui,我有一个播放/暂停按钮,按下时会发生变化。目前,它只是淡入淡出,但我希望它执行一个动画,看起来新的颜色是流动的旧的。 这就是我到目前为止所做的: if(meditationViewModel.timerIsRunning){ HStack{ Image(systemName: "pause") Text("STOP") .bold() } .padding()

我有一个播放/暂停按钮,按下时会发生变化。目前,它只是淡入淡出,但我希望它执行一个动画,看起来新的颜色是流动的旧的。

这就是我到目前为止所做的:

if(meditationViewModel.timerIsRunning){
    HStack{
        Image(systemName: "pause")
        Text("STOP")
            .bold()
     }
      .padding()
      .foregroundColor(.white)
      .background(Color.red)
      .cornerRadius(25)
      .shadow(radius: 20)
}else{
    HStack{
        Image(systemName: "play")
        Text("PLAY")
            .bold()
    }
     .padding()
     .foregroundColor(.white)
     .background(Color.green)
     .cornerRadius(25)
}

冥想视图模型.timerIsRunning的更改发生在其他地方。

这里是一个可能方法的演示-在这种情况下,只需在文本和不透明背景之间使用一些形状圆圈,并根据状态移动它

使用Xcode 12.4/iOS 14.4编写

注意:您可以使用点击手势而不是按钮来调整所有参数和颜色,但总体上想法保持不变


伟大的解决方案!看起来棒极了
struct DemoView: View {
    @State private var playing = false
    var body: some View {
        Button(action: { playing.toggle() }) {
            HStack{
                Image(systemName: playing ? "pause" : "play")
                Text(playing ? "STOP" : "PLAY")
                    .bold()
            }
            .background(
                Circle().fill(Color.purple)
                    .frame(width: 160, height: 160, alignment: .leading)
                    .blur(radius: 3)
                    .offset(x: playing ? 0 : -160, y: -40)
                    .animation(.easeInOut(duration: 1), value: playing)
            )
            .padding()
            .foregroundColor(.white)
            .background(Color.green)
            .cornerRadius(25)
            .clipped()
            .shadow(radius: 20)
        }
    }
}