首页 > 解决方案 > 按下时如何为所有选项卡图标(不仅仅是一个)设置动画?

问题描述

我正在使用 if 语句,但它仅适用于通知铃图标,如何将相同的效果应用于其他图标?我应该使用 switch 语句吗?

  if selection == index {
                        Color(.label)
                            .frame(height: 2)
                            .offset(y: -8)
                            .matchedGeometryEffect(id: "currentTab", in: currentTab)
                    }
                    if tabs[selection].label == "Notifications" && tabs[index].label  == "Notifications" {
                        Image(systemName: tabs[index].image)
                            .frame(height: 20)
                            .rotationEffect(.degrees(25))
                        
                    }   else {
                        Image(systemName: tabs[index].image)
                            .frame(height: 20)
                            .rotationEffect(.degrees(0))
                        
                    }
                    
                    
                    Text(tabs[index].label) .font(.caption2)
                        .fixedSize()
                        .foregroundColor(.blue)
              }
                .fixedSize(horizontal: false, vertical: true)
                .frame(width: geometry.size.width / 2, height: 44, alignment: .bottom).padding(.horizontal).foregroundColor(selection == index ? Color(.label) : .secondary )
                .onTapGesture{
                    withAnimation{selection = index}
                }
            }
        
        }
    }.frame(height: 44, alignment: .bottom)
}

}

在此处输入图像描述

标签: iosxcodeswiftui

解决方案


推荐阅读