diff options
Diffstat (limited to 'Sora/Views/ZoomableImageView.swift')
| -rw-r--r-- | Sora/Views/ZoomableImageView.swift | 89 |
1 files changed, 0 insertions, 89 deletions
diff --git a/Sora/Views/ZoomableImageView.swift b/Sora/Views/ZoomableImageView.swift deleted file mode 100644 index 8304c6e..0000000 --- a/Sora/Views/ZoomableImageView.swift +++ /dev/null @@ -1,89 +0,0 @@ -import SwiftUI - -struct ZoomableImageView: View { - let image: Image - @State private var screenWidth = 0.0 - @State private var screenHeight = 0.0 - @State private var currentScale = 1.0 - @State private var previousScale = 0.0 - @State private var currentOffset: CGSize = .zero - @State private var previousOffset: CGSize = .zero - - var body: some View { - GeometryReader { geometry in - VStack { - image - .resizable() - .scaledToFit() - .scaleEffect(currentScale) - .offset(currentOffset) - .frame(width: screenWidth, height: screenHeight) - .clipped() - .gesture( - MagnifyGesture() - .onChanged { gesture in - withAnimation(.interactiveSpring()) { - currentScale = - previousScale + gesture.magnification - (previousScale == 0 ? 0 : 1) - } - } - .onEnded { _ in - previousScale = currentScale - } - .simultaneously( - with: DragGesture(minimumDistance: 0) - .onChanged { gesture in - withAnimation(.interactiveSpring()) { - var newOffset: CGSize = .zero - let offset = gesture.translation - - newOffset.width = offset.width + previousOffset.width - newOffset.height = offset.height + previousOffset.height - - currentOffset = clampOffset(offset: newOffset) - } - } - .onEnded { _ in - previousOffset = currentOffset - } - ) - ) - .highPriorityGesture( - TapGesture(count: 2) - .onEnded { - withAnimation { - currentScale = 1.0 - previousScale = 0 - currentOffset = .zero - previousOffset = .zero - } - } - ) - } - .onAppear { - screenWidth = geometry.size.width - screenHeight = geometry.size.height - } - } - } - - private func clampOffset(offset: CGSize = .zero) -> CGSize { - var newOffset = offset - - if currentScale > 1 { - let maxX = ((screenWidth * currentScale) - screenWidth) / 2 - let maxY = ((screenHeight * currentScale) - screenHeight) / 2 - - newOffset.width = min(max(-maxX, newOffset.width), maxX) - newOffset.height = min(max(-maxY, newOffset.height), maxY) - } else { - newOffset = .zero - } - - return newOffset - } -} - -#Preview { - ZoomableImageView(image: Image(systemName: "photo")) -} |