我有 2 个自定义按钮样式,我想在点击按钮时更改样式。我尝试了这样的方法:
Button(action: {
self.pressed.toggle()
})
{
Text("Button")
}.buttonStyle(pressed ? style1() : style2())
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它从它所属的 VStack 中给了我一个错误:
Unable to infer complex closure return type; add explicit type to disambiguate
Run Code Online (Sandbox Code Playgroud)
如果我做类似的事情:
.buttonStyle(style1())
Run Code Online (Sandbox Code Playgroud)
或者
.buttonStyle(style2())
Run Code Online (Sandbox Code Playgroud)
然后错误消失,因此它不是来自 style1() 或 style2()。
我有一个 SwiftUI 文本标签,我想在按下按钮后在其中写一些东西。
这是我的代码:
Button(action: {
registerRequest() // here is where the variable message changes its value
}) {
Text("SignUp")
}
Text(message) // this is the label that I want to change
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我正在 SwiftUI 中为视图设置动画,即使我在 .onAppear() 方法中没有它,它也会在视图出现时立即设置动画。我希望它只在我按下文本时才动画,这就是我使用点击手势的原因。这是我的代码:
struct ContentView: View {
var body: some View {
Text()
.scaleEffect(cardTap ? 0.9 : 1)
.gesture(LongPressGesture().onChanged { value in
self.cardTap = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.cardTap = false
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
}
}
).animation(.spring(response: 0.5, dampingFraction: 0.5, blendDuration: 0))
}
}
Run Code Online (Sandbox Code Playgroud) 我想从使用 a 显示为列表的数组中删除一个元素ForEach
,但我还需要向 a 发送HTTP
请求,REST API
并且需要将元素的索引放入请求正文中。这是我的代码:
ForEach(self.symptoms, id: \.self) { symptom in
VStack(alignment: .leading) {
Text(symptom)
}
}.onDelete(perform: delete)
Run Code Online (Sandbox Code Playgroud)
这是删除功能:
func delete(at offsets: IndexSet) {
self.symptoms.remove(atOffsets: offsets)
// here I want to make the HTTP request
}
Run Code Online (Sandbox Code Playgroud) 我想制作文字“你好,世界!” 一旦景色出现,就慢慢地、不停地上下浮动。
import SwiftUI
struct animate: View {
var body: some View {
Text("Hello, World!")
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你!
我有一些已经完成的文本字段,我想添加一些“编辑”按钮,这些按钮将向我按下的按钮显示键盘。以下是其中一个文本字段的代码:
Image(colorScheme == .light ? "user" : "userDark").resizable().frame(width: 30, height: 30)
TextField("Name", text: $name).textFieldStyle(PlainTextFieldStyle()).padding(.leading, 5).font(.system(size: 20))
.autocapitalization(.words)
.disableAutocorrection(true)
if name != localName {
Button(action: {
self.name = ""
}) {
Text("Update").font(.system(size: 15)).fontWeight(.light)
.foregroundColor(colorScheme == .light ? Color.black : Color.white)
}
} else if name == localName {
Text("Edit").font(.system(size: 15)).fontWeight(.light)
.foregroundColor(colorScheme == .light ? Color.black : Color.white)
}
Run Code Online (Sandbox Code Playgroud)