Picker/选择器, ColorPicker/颜色选择器, DatePicker/日期选择器 的使用

1. Picker 选择器的使用

  1.1 实现

/// 选择器
struct PickerBootcamp: View {
    @State var selection: String = "Most Recent"
    
    let filterOptions:[String] = [
        "Most Recent", "Most Popular", "Most Liked"
    ]
    
    init(){
        UISegmentedControl.appearance().selectedSegmentTintColor = UIColor.red
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor : UIColor.white
        ]
        UISegmentedControl.appearance().setTitleTextAttributes(attributes, for: .selected)
    }
    
    var body: some View {
        //packer1
        //picker2
        picker3
    }
    
    /// 选择器 三
    var picker3: some View {
        Picker(
            selection: $selection) {
                ForEach(filterOptions.indices, id: \.self) { index in
                    Text(filterOptions[index]).tag(filterOptions[index])
                }
            } label: {
                Text("Picker")
            }
            .pickerStyle(.segmented)
        //.background(Color.blue)
    }
    
    /// 选择器 二
    var picker2: some View{
        Picker(
            selection: $selection) {
                ForEach(filterOptions, id: \.self) { option in
                    HStack {
                        Text(option)
                        Image(systemName: "heart.fill")
                    }
                    .tag(option)
                }
            } label: {
                HStack{
                    Text("Filter:")
                    Text(selection)
                }
                .font(.headline)
                .foregroundColor(.white)
                .padding()
                .padding(.horizontal)
                .background(Color.blue)
                .cornerRadius(10)
                .shadow(color: Color.blue.opacity(0.3), radius: 10, x: 0, y: 10)
            }
            .pickerStyle(.menu)
    }
    
    /// 选择器 一
    var packer1: some View{
        VStack{
            HStack {
                Text("Age:")
                Text(selection)
            }
            Picker(selection: $selection) {
                ForEach(0..<5) { number in
                    Text(number.description)
                        .foregroundColor(.blue)
                        .tag("\(number)")
                }
            } label: {
                Text("Picker")
            }
            .background(Color.gray.opacity(0.3))
            .pickerStyle(.wheel)
        }
    }
}

  1.2 效果图:

    

2. ColorPicker 颜色选择器的使用

  2.1 实现

/// 颜色选择器
struct ColorPickerBootcamp: View {
    @State var backgroundColor: Color = .green
    
    var body: some View {
        ZStack {
            backgroundColor.ignoresSafeArea()
            ColorPicker( "Select a color",
                         selection: $backgroundColor,
                         supportsOpacity: true)
            .padding()
            .background(Color.black)
            .cornerRadius(10)
            .foregroundColor(.white)
            .padding(.horizontal, 50)
            .font(.headline)
        }
    }
}

  2.2 效果图:

3. DatePicker 日期选择器的使用

  3.1 实现

/// 时间选择器
struct DatePickerBootcamp: View {
    @State var selectedDate: Date = Date()
    // 开始时间
    let startingDate: Date = Calendar.current.date(from: DateComponents(year: 2020)) ?? Date()
    // 结束时间
    let endingDate: Date = Date()
    // 时间格式
    var dateFormatter: DateFormatter{
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .medium
        return formatter
    }
    
    var body: some View {
        //picker1
        //picker2
        picker3
    }
    
    // 方式三
    var picker3: some View{
        VStack{
            Text("Selected date is:")
            Text(dateFormatter.string(from: selectedDate))
                .font(.title)
            DatePicker("Select a date", selection: $selectedDate, in: startingDate...endingDate, displayedComponents: [.date])
                .accentColor(Color.red)
                .datePickerStyle(.compact)
        }
    }
    
    // 方式二
    var picker2: some View{
        //.date, .hourAndMinute
        DatePicker("Select a Date", selection: $selectedDate, displayedComponents:[.date, .hourAndMinute])
            .accentColor(Color.red)
            .datePickerStyle(.compact)
    }
    
    // 方式一
    var picker1: some View{
        DatePicker("Select a Date", selection: $selectedDate)
            .accentColor(Color.red)
            .datePickerStyle(
                // .compact
                // .graphical
                .wheel
            )
    }
}

   3.2 效果图:

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hanyang Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值