struct ContentView: View {
@State var rotation = 0.0
@State var color = Color.red
var body: some View {
Rectangle()
.fill(color)
.frame(width: 200, height: 200)
.rotationEffect(.degrees(rotation))
.animation(.easeInOut(duration: 2).delay(0.1), value: rotation)
.onTapGesture {
rotation += 360
if color == Color.red {
color = Color.blue
} else {
color = Color.red
}
}
}
}
// color == Color.red ? color = Color.blue : color = Color.red -nope!
/*
.onTapGesture {
rotation += 360
if color == Color.red {
color = Color.blue
} else {
color = Color.red
}
}
*/
我已经尝试过大括号、括号等一堆东西,但我无法将这个直接的“if”变成三元。可以吗?
越来越多的细节,如果我可以的话。
我以为我做得更好了,然后我想添加旋转方向,然后我又回到了坑里。
struct ContentView: View {
@State var rotation = 0.0
@State var color = Color.red
@State var isOn: Bool = false
var body: some View {
Rectangle()
.fill(isOn ? .blue : .red)
.frame(width: 200, height: 200)
.rotationEffect(.degrees(rotation))
.animation(.easeInOut(duration: 1).delay(0.1), value: rotation)
.onTapGesture {
isOn.toggle()
if isOn { rotation += 360.0 } else { rotation -= 360.0 }
}
}
}
我也会去快速手册-三元运算符部分。