簡體   English   中英

長按 animation 與 Swift UI

[英]Long press animation with Swift UI

我有一個長按手勢,突出顯示另一個按鈕。 代碼如下所示:

@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重音並返回更平滑的過渡? 目前它切換得相當突然。

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

備份

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM