iOS 平台的单选按钮组件(Swift UI)

1、自定义一个RadioButton组件的View视图

struct RadioButton:view{
    let selfID: string
    let selectedID: String

    init(selfID: String,selectedID: String){
        self.selfID= selfID
        self.selectedID=selectedID
    }

    var body: some View{
        Button(action:{
        self.selectedID=self.selfID
        },label:{
            HStack(alignment:.center,spacing:12,content:{
                Text(self.id)
                .frame(width: 60,height:50)
                .background(Color.blue)
                .foregroundColor(self.selectedID == self.selfID? Color.blue : Color.black)
                .background(Color.blue)
                .cornerRadius(10)//设置按钮的圆角

            })
        })

    }

}

selfID:当前RadioButton自身ID

selectedID:当前被选中的RadioButton的ID

Button:RadioButton被点击时的回调函数,作用把selfID赋值给selectedID

2、在主View视图里面对上定义的RadioButtonView组件进行调用

let items:[String]=["色盘循环","色温循环",“亮度循环",“闪烁"]

@State var selectedID = ""

HStackt{
       ForEach(items,id:\.self){item in
         RadioButton(id: item,selectedID: self,selectedID)

        }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值