点击 Image SwiftUI 上的动画

Tap animation on Image SwiftUI

如何在图像上制作动画,当我点击它并按住它时,它会向后缩放,当我释放它时,图像 return 回到其原始大小?

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

这是一个可能的解决方案演示。测试 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
                    }
            })
    }
}

backup