swiftui选取器标签可视化

sy5wg1nm  于 2022-12-10  发布在  Swift
关注(0)|答案(1)|浏览(189)

我在swiftui菜单内的选择器的标签可视化中遇到了问题,当选择将文本接缝更改为文字动画时,它会缓慢地显示为点......并且在显示所有文本后,但我不知道为什么要这样做,这是代码,我附上了一个简短的gif来演示该问题,它会出现在画布上,但也会出现在模拟器和真实的设备中

struct AddCostView: View {
    
    @State private var transactionSelectionPicker: TransactionType = .outgoing
    @State private var pickerPressed: Bool = false
    @State private var backgroundColor: String = "Outgoing"
    
    var body: some View {
        NavigationView {
            
            ZStack {
                Color(backgroundColor)
                    .ignoresSafeArea(.container, edges: .top)
                VStack {
                    HStack {
                        Text("Tipo di transazione:")
                            .font(.headline)
                            .foregroundColor(.secondary)
                        
                        Spacer()
                        Menu {
                            Picker("transazioni", selection: $transactionSelectionPicker) {
                                ForEach(TransactionType.allCases, id: \.rawValue) { item in
                                    Text(item.rawValue)
                                        .tag(item)
                                }
                            }
                            .labelsHidden()
                            .pickerStyle(.inline)
                            .onChange(of: transactionSelectionPicker) { newValue in
                                pickerPressed.toggle()
                                switch newValue {
                                case .outgoing:
                                    backgroundColor = "Outgoing"
                                case .incoming:
                                    backgroundColor = "Incoming"
                                case .transfer:
                                    backgroundColor = "Transfer"
                                case .currecyChange:
                                    backgroundColor = "CurrencyChange"
                                }
                            }
                        } label: {
                            HStack(spacing: 8) {
                                
                                Text(transactionSelectionPicker.rawValue)
                                    .foregroundColor(Color(UIColor.label))
                                    .bold()
                                    
                                Image(systemName: "chevron.right")
                                    .foregroundColor(.secondary)
                                    .rotationEffect(pickerPressed ? Angle(degrees: 90) : Angle(degrees: 0))
                                    .font(.headline)
                                
                                
                            }
                            .padding()
                            
                            
                        }
                        .frame(width: 170, alignment: .leading)
                        .background(
                            .ultraThickMaterial, in: RoundedRectangle(cornerRadius: 8, style: .continuous)
                        )
                        .onTapGesture {
                            pickerPressed.toggle()
                        }
                        
                    }
                    .padding()
                    
                    List {
                        Text("t")
                    }
                }
                
                
                
            }
            
            
            
            .navigationTitle("Transazione")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

foreach是使用枚举完成的,我没有动画或事务
感谢所有能帮助我的人

jmo0nnb3

jmo0nnb31#

我也遇到过类似的问题,解决这个问题的方法是使用.fixedSize()修饰符,这样就解决了当视图变大时获取...的问题,但是当它收缩的时候看起来有点奇怪。因此,我创建了一个状态变量,并使用.onChnage(of: value)检查新值是更长还是更短,然后仅在以下情况下应用.fixedSize()新的值更长。下面是它的大致外观。

@State private var isLonger = true
@State private var value = "initial value"
...

// value here is the variable that stores the current selection value for the picker
var body: some View {
  PickerView
    .onChange(of: value) { [value] newValue in
      isLonger = newValue.count > value.count
    }

}

var PickerView: some View {
  if isLonger {
    MenuPicker()
      .fixedSize()
  } else {
    MenuPicker()
  }
}

相关问题