我有一个长按手势突出显示另一个按钮。代码如下所示:
@GestureState var highlight = false
var body: some View {
var longPress: some Gesture {
LongPressGesture(minimumDuration: 3)
.updating($highlight) { currentstate, gestureState, transaction in
gestureState = currentstate
transaction.animation = Animation.easeInOut(duration: 2.0)
}
}
Text("highlight!")
.gesture(longPress)
Button(...) { ... }
.accentColor(self.highlight ? .green : .none)
}
Run Code Online (Sandbox Code Playgroud)
我如何确保从.none重音到.green重音和后退的过渡更加平滑?目前它切换得相当突然。
修饰符.accentColor不可设置动画,但是您可以使用以下方法实现相同的效果(据我了解)。
struct TestLongPressGesture: View {
@GestureState var highlight = false
var body: some View {
var longPress: some Gesture {
LongPressGesture(minimumDuration: 3)
.updating($highlight) { currentstate, gestureState, transaction in
transaction.animation = Animation.easeInOut(duration: 2.0)
gestureState = currentstate
}
}
return VStack {
Text("highlight!")
.gesture(longPress)
Divider()
Button("Button") { }
.font(Font.largeTitle.bold())
.foregroundColor(.white)
.colorMultiply(self.highlight ? .green : .blue)
.animation(.easeInOut, value: highlight)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |