【问题标题】:Tap animation on Image SwiftUI在 Image SwiftUI 上点击动画
【发布时间】:2020-07-31 14:20:17
【问题描述】:

如何在图像上制作动画,这样当我点击并按住它时它会向后缩放,当我松开它时,图像会恢复到原来的大小?

struct ContentView: View {
    var body: some View {
        Image(systemName: "heart")
        .onTapGesture {
        // Gesture when held down and released
        }
    }
}

【问题讨论】:

    标签: swift image animation swiftui gesture


    【解决方案1】:

    这是一个可能解决方案的演示。使用 Xcode 12 / iOS 14 测试。

    struct DemoImageScale: View {
        @GestureState private var isDetectingPress = false
    
        var body: some View {
            Image("plant")
                .resizable().aspectRatio(contentMode: .fit)
                .scaleEffect(isDetectingPress ? 0.5 : 1)
                .animation(.spring())
                .gesture(LongPressGesture(minimumDuration: 0.1).sequenced(before:DragGesture(minimumDistance: 0))
                    .updating($isDetectingPress) { value, state, _ in
                        switch value {
                            case .second(true, nil):
                                state = true
                            default:
                                break
                        }
                })
        }
    }
    

    【讨论】:

    • 不错的一个。我什至不知道存在@GestureState 属性包装器。谢谢!
    • 效果不错,只是有点慢...如何加快动画速度?
    • @xmetal 更改动画部分,例如。 .animation(.easeInOut(duration: 0.3))
    • @pawello2222 我刚刚意识到这里可能会改变的是 LongPressGesture。我尝试使用 TapGesture,因为我需要在点击图像后立即看到动画,但如果我这样做会丢失 scaleEffect 动画
    • @xmetal,TapGesture 是瞬时的,因此它不适用于您的情况。尝试调整 LongPressGesture(minDuration: ) 的更新,以及任何其他修饰符,如上面提到的动画。
    猜你喜欢
    • 1970-01-01
    • 2022-11-30
    • 2017-08-30
    • 1970-01-01
    • 2019-01-06
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多