You need to enable JavaScript to run this app. SwiftUI:如何通过枚举为不同按钮case分配差异化动画样式
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI:如何通过枚举为不同按钮case分配差异化动画样式

我有一个ForEach循环,用于展示所有带图标的Button,这些图标来源于Icons枚举。每次点击某一按钮后它会保持激活状态,直到我点击其他按钮,目前所有按钮case的动画样式都是统一的。

enter image description here

enum Icons: String, CaseIterable, Hashable {
    case overlayText = "Text"
    case image = "Image"
    case rotate = "Rotate"
    case audio = "Audio"
    case merge = "Merge"
    case split = "Split"
    case duplicate = "Duplicate"
    case none
}

struct EffectPanel: View {
    @State var currentIconSelected: Icons = .none
    @State var listIcons = [Bool](repeating: false, count: Icons.allCases.count)
    var body: some View {
        VStack {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 10) {
                    ForEach(Icons.allCases, id: \.self) { i in
                        if i != .none {
                            EffectIcon(icon: Icons(rawValue: i.rawValue)!, currentIconSelected: $currentIconSelected)
                        }
                    }
                }
            }
            .background(Color.black)
        }
    }
}

现在我想为3个指定按钮添加第二种动画样式,例如对应枚举case为rotate、audio、merge的按钮。目前我是通过以下硬编码判断方式,手动为"rotate"这个case应用第二种动画样式:

if currentIconSelected.rawValue == "Rotate"

以下是我的EffectIcon视图代码:

struct EffectIcon: View {
    
    var icon: Icons = .none
    @Binding var currentIconSelected: Icons
    
    var isSelected: Bool {
        if icon == currentIconSelected { return true }
        return false
    }
    
    @State var buttonFlash: Bool = true
    
    var body: some View {
        HStack(spacing: 0) {
            Button(action: {
                self.currentIconSelected = self.icon
            }) {
                ZStack {
                    if isSelected {
                        // ---- begin manually applying animation ---- //
                        if currentIconSelected.rawValue == "Rotate" {
                            HStack {
                                if buttonFlash {
                                    Image("\(Icons.allCases[1].rawValue)")
                                        .font(.title2)
                                        .frame(width: 65, height: 70)
                                } else {
                                    Image("\(Icons.allCases[1].rawValue)Active")
                                        .font(.title2)
                                        .frame(width: 65, height: 70)
                                }
                            }
                            .onTapGesture {
                                self.colorChange()
                            }
                            // ---- end manually applying animation ---- //
                        } else {
                            Image("\(icon.rawValue)Active")
                                .font(.title2)
                                .frame(width: 65, height: 70)
                            Text("\(icon.rawValue)")
                                .fontWeight(.bold)
                                .font(.system(size: 12))
                                .foregroundColor(Color("orange"))
                                .frame(alignment: .leading)
                                .lineLimit(1)
                                .padding(.top, 56)
                        }
                    } else {
                        Image("\(icon.rawValue)")
                            .font(.title2)
                            .frame(width: 65, height: 70)
                    }
                }
            }
        }
    }
    
    private func colorChange() {
        self.buttonFlash.toggle()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(Int(0.1)), execute: {
            withAnimation(.easeIn) {
                self.buttonFlash.toggle()
            }
        })
    }
}

我知道存在更优雅的实现方案(比如借助额外的枚举搭配初始化方法来实现),但我还没有摸索出具体的实现方式。非常感谢你的帮助!

内容的提问来源于stack exchange,提问作者Duc Dang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.11 20:36:02