【问题标题】:Turn buttons inactive and change opacity使按钮处于非活动状态并更改不透明度
【发布时间】:2020-07-04 09:04:36
【问题描述】:

我对 Swift 还很陌生,所以请多多包涵。

screenshot when nothing's tapped

screenshot when one button is tapped

我正在尝试转动其他按钮,这些按钮未被点击、处于非活动状态并将不透明度更改为 0.5。

到目前为止,我已经尝试过使用 $bindings,但这并没有按照我想要的方式工作。

这是我的 SelectionButtons 的代码:

struct SelectionButton: View {

var buttonText = "Selection Button"

var buttonColor = Color.white
var buttonWidth: CGFloat = 150
var active = false


var body: some View {
    
    ZStack {
        
        if active {
            
            RoundedRectangle(cornerRadius: 45)
                .frame(width: buttonWidth, height: 50)
                .foregroundColor(Color("neonGreen"))
                .shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.25)), radius:9, x:0, y:0)
            
            
            Text(buttonText)
                .font(.system(size: 18))
                .foregroundColor(.black)
                .bold()

            
        }else{
            RoundedRectangle(cornerRadius: 45)
                .frame(width: buttonWidth, height: 50)
                .foregroundColor(buttonColor)
                .shadow(color: Color(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.25)), radius:9, x:0, y:0)
            
            
            Text(buttonText)
                .font(.system(size: 18))
                .foregroundColor(.black)
            
        }
        
        
    }
    
}

这就是我使用它们的方式:

@State var gelbesBlatt = false

var body: some View {
    
    NavigationView {
        
        
        
        Button(action: {
            self.gelbesBlatt.toggle()
        }, label: {SelectionButton(buttonText: "gelb", buttonColor: .white, buttonWidth: 150, active: gelbesBlatt)
        })
    }
    
    
}

谢谢!

如果您知道我可以如何添加组合,以便可以一起选择属于一起的特定按钮(例如“Ganzes Blatt > gelb”和“Ränder > braun”),那就太棒了!

【问题讨论】:

    标签: ios button binding swiftui


    【解决方案1】:

    检查一下:

        @State var gelbesBlatt = false
        @State var btnBraun = false
        var body: some View {
            NavigationView {
                HStack {
                    Button(action: {
                        self.toggleButton(button: 0)
                    }, label: {SelectionButton(buttonText: "gelb", buttonColor: .white, buttonWidth: 150, active: gelbesBlatt)
                    })
                    
                    Button(action: {
                        self.toggleButton(button: 1)
                    }, label: {SelectionButton(buttonText: "braun", buttonColor: .white, buttonWidth: 150, active: btnBraun)
                    })
                }
            }
        }
        func toggleButton(button: Int) {
            if(button == 0) {
            self.gelbesBlatt = true
            self.btnBraun = false
        } else {
            self.gelbesBlatt = false
            self.btnBraun = true
        }
            self.gelbesBlatt.toggle()
            self.btnBraun.toggle()
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多