Swift UI 实现长按动画

5

我有一个长按手势,可以高亮显示另一个按钮。代码如下:

@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)                   
}

我该如何确保从.none强调到.green强调的过渡更加平滑,以及从.green强调回到.none的过渡也更加平滑?目前切换相对突然。

1个回答

2
.accentColor”不是可动画化的修改器,但是你可以通过以下方法实现相同的效果(如我所理解的)。

demo

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)
        }
    }
}

它看起来好多了,但我只看到 easeOut 动画。而且在暗模式下,乘法产生的颜色相当紫色,而非绿色。 - vasily
我花了一些时间才让它正常工作。你的按钮上缺少一个额外的.animation(.easeInOut)colorMultiply之后。 - vasily

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接