Files
hapi/ios/Hapi/Features/Chat/ImageZoomView.swift
T
wushenghuaandHAPI 0d8abb7e2c feat(ios): zoom image attachments full screen
Tapping a user attachment or a generated image opens a pannable,
pinch-zoomable viewer. Zoom math (fit scale, clamping, double-tap
targets) is pure and unit tested so the gestures stay predictable.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
2026-12-05 12:09:07 +08:00

161 lines
5.5 KiB
Swift

import SwiftUI
import UIKit
/// Zoom bounds for the full-screen image viewer. Pure so the drag clamps can
/// be tested without gesture plumbing.
enum ImageZoomMath {
static let minScale: CGFloat = 1
static let maxScale: CGFloat = 8
static func clampedScale(_ value: CGFloat) -> CGFloat {
min(maxScale, max(minScale, value))
}
/// Aspect-fit size of the image inside `container`.
static func fittedSize(for imageSize: CGSize, in container: CGSize) -> CGSize {
guard imageSize.width > 0, imageSize.height > 0,
container.width > 0, container.height > 0 else { return .zero }
let imageAspect = imageSize.width / imageSize.height
let containerAspect = container.width / container.height
if imageAspect > containerAspect {
return CGSize(width: container.width, height: container.width / imageAspect)
}
return CGSize(width: container.height * imageAspect, height: container.height)
}
/// Keep the scaled image edges from drifting inside the viewport.
static func clampedOffset(
_ offset: CGSize,
scale: CGFloat,
imageSize: CGSize,
container: CGSize
) -> CGSize {
let fitted = fittedSize(for: imageSize, in: container)
let maxX = max(0, (fitted.width * scale - container.width) / 2)
let maxY = max(0, (fitted.height * scale - container.height) / 2)
return CGSize(
width: min(maxX, max(-maxX, offset.width)),
height: min(maxY, max(-maxY, offset.height))
)
}
}
/// Full-screen image with pinch zoom, drag panning at >1x, and double-tap
/// zoom. Used by generated media and user-message attachments.
struct ZoomableImageViewer: View {
let image: UIImage
let title: String
@Environment(\.dismiss) private var dismiss
@State private var scale: CGFloat = 1
@State private var lastScale: CGFloat = 1
@State private var offset: CGSize = .zero
@State private var lastOffset: CGSize = .zero
var body: some View {
GeometryReader { proxy in
let container = proxy.size
ZStack {
Color.black.ignoresSafeArea()
Image(uiImage: image)
.resizable()
.scaledToFit()
.scaleEffect(scale)
.offset(offset)
.gesture(magnification(container: container))
.simultaneousGesture(drag(container: container))
.onTapGesture(count: 2) {
toggleZoom(container: container)
}
.accessibilityLabel(Text(verbatim: title))
}
.overlay(alignment: .topTrailing) {
Button {
dismiss()
} label: {
Image(systemName: "xmark.circle.fill")
.font(.title2)
.foregroundStyle(.white.opacity(0.85))
.frame(width: 44, height: 44)
.contentShape(Rectangle())
}
.padding(.top, 4)
.padding(.trailing, 8)
.accessibilityLabel("Close")
}
}
.statusBarHidden()
}
private func magnification(container: CGSize) -> some Gesture {
MagnifyGesture()
.onChanged { value in
scale = ImageZoomMath.clampedScale(lastScale * value.magnification)
offset = ImageZoomMath.clampedOffset(
offset,
scale: scale,
imageSize: image.size,
container: container
)
}
.onEnded { _ in
lastScale = scale
if scale <= ImageZoomMath.minScale {
resetZoom()
} else {
offset = ImageZoomMath.clampedOffset(
offset,
scale: scale,
imageSize: image.size,
container: container
)
lastOffset = offset
}
}
}
private func drag(container: CGSize) -> some Gesture {
DragGesture()
.onChanged { value in
guard scale > ImageZoomMath.minScale else { return }
offset = ImageZoomMath.clampedOffset(
CGSize(
width: lastOffset.width + value.translation.width,
height: lastOffset.height + value.translation.height
),
scale: scale,
imageSize: image.size,
container: container
)
}
.onEnded { _ in
lastOffset = offset
}
}
private func toggleZoom(container: CGSize) {
withAnimation(.easeInOut(duration: 0.2)) {
if scale > ImageZoomMath.minScale {
resetZoom()
} else {
scale = 2.5
lastScale = 2.5
offset = ImageZoomMath.clampedOffset(
.zero,
scale: scale,
imageSize: image.size,
container: container
)
lastOffset = offset
}
}
}
private func resetZoom() {
scale = ImageZoomMath.minScale
lastScale = ImageZoomMath.minScale
offset = .zero
lastOffset = .zero
}
}