SwiftUI外观动画

问题描述

我正在使用出现在动画屏幕上的屏幕键盘。我的ButtonStyle定义如下:

struct KeyboardSquareButtonStyle: ButtonStyle {
    var enabledColor: Color = Color.AppColor.Keyboard.regularButtonActive
    var disabledColor: Color = Color.AppColor.Keyboard.regularButtonInactive
    
    func makeBody(configuration: Self.Configuration) -> some View {
        ConfigurationWrappedKeyboardButton(configuration: configuration,enabledColor: enabledColor,disabledColor: disabledColor)
            .aspectRatio(1,contentMode: .fit)
    }
}

struct ConfigurationWrappedKeyboardButton: View {
    let configuration: KeyboardRectangleButtonStyle.Configuration
    var enabledColor: Color
    var disabledColor: Color
    
    @Environment(\.isEnabled) private var isEnabled: Bool
    
    var body: some View {
        configuration.label
            .font(Font.system(size: 50,weight: .light,design: .default))
            .foregroundColor(Color.AppColor.primaryText)
            .frame(minWidth: 45,maxWidth: .infinity,minHeight: 45,maxHeight: .infinity,alignment: .center)
            .background(backgroundColor(pressed: configuration.ispressed))
            .animation(.eaSEOut(duration: 0.1))
    }
    
    private func backgroundColor(pressed: Bool) -> Color {
        guard !pressed else {
            return enabledColor.opacity(0.7)
        }
        
        return isEnabled ? enabledColor : disabledColor
    }
}

动画按钮状态对我来说至关重要。

键盘一个HStack,其中包含两个vstack,它们都有自己的外观转换:

struct Example: View {
    @State private var keyboardShown: Bool = false
    
    var body: some View {
        ZStack {
            vstack {
                Button("Toggle keyboard") { self.keyboardShown.toggle() }
                Spacer()
            }
            
            if keyboardShown {
                Keyboard()
                    .transition(.move(edge: .leading))
                    .animation(.easeIn)
            }
        }
    }
}

struct Keyboard: View {
    var body: some View {
        HStack(spacing: 1) {
            vstack(spacing: 1) {
                Button("Button") {}
                Color.green
            }
            
            vstack(spacing: 1) {
                Color.blue
                Color.yellow
            }
        }
        .buttonStyle(KeyboardSquareButtonStyle())
        .aspectRatio(1,contentMode: .fit)
    }
}

以上代码如果功能完整,可以启动进行测试。我现在的实际结果是,当Button忽略父animation设置并使用ButtonStyle中指定的animation进行父转换时,三个彩色视图以相同的速度和过渡出现。有没有办法让ButtonStyle逻辑不影响整个键盘外观的转换?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...