|
| 1 | +import SwiftUI |
| 2 | +import FLAnimatedImage |
| 3 | + |
| 4 | +struct AnimatedImageView: UIViewRepresentable { |
| 5 | + let data: Data |
| 6 | + |
| 7 | + func makeUIView(context: Context) -> FLAnimatedImageView { |
| 8 | + let imageView = FLAnimatedImageView() |
| 9 | + imageView.contentMode = .scaleAspectFit |
| 10 | + imageView.clipsToBounds = true |
| 11 | + return imageView |
| 12 | + } |
| 13 | + |
| 14 | + func updateUIView(_ imageView: FLAnimatedImageView, context: Context) { |
| 15 | + if let animatedImage = FLAnimatedImage(animatedGIFData: data) { |
| 16 | + imageView.animatedImage = animatedImage |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +struct AnimatedImageScrollView: UIViewRepresentable { |
| 22 | + let data: Data |
| 23 | + |
| 24 | + func makeCoordinator() -> Coordinator { |
| 25 | + Coordinator(self) |
| 26 | + } |
| 27 | + |
| 28 | + func makeUIView(context: Context) -> UIScrollView { |
| 29 | + let scrollView = CustomScrollView() |
| 30 | + scrollView.delegate = context.coordinator |
| 31 | + |
| 32 | + let imageView = FLAnimatedImageView() |
| 33 | + if let animatedImage = FLAnimatedImage(animatedGIFData: data) { |
| 34 | + imageView.animatedImage = animatedImage |
| 35 | + } |
| 36 | + imageView.contentMode = .scaleAspectFit |
| 37 | + imageView.frame = CGRect(origin: .zero, size: imageView.animatedImage?.size ?? .zero) |
| 38 | + scrollView.addSubview(imageView) |
| 39 | + |
| 40 | + context.coordinator.imageView = imageView |
| 41 | + context.coordinator.scrollView = scrollView |
| 42 | + |
| 43 | + let doubleTapGesture = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleDoubleTap(_:))) |
| 44 | + doubleTapGesture.numberOfTapsRequired = 2 |
| 45 | + scrollView.addGestureRecognizer(doubleTapGesture) |
| 46 | + |
| 47 | + // Calculate initial zoom scale |
| 48 | + if let size = imageView.animatedImage?.size { |
| 49 | + let widthScale = UIScreen.main.bounds.width / size.width |
| 50 | + let heightScale = UIScreen.main.bounds.height / size.height |
| 51 | + let minScale = min(widthScale, heightScale) |
| 52 | + |
| 53 | + scrollView.minimumZoomScale = minScale |
| 54 | + scrollView.maximumZoomScale = 5.0 |
| 55 | + |
| 56 | + // Set content size to image size |
| 57 | + scrollView.contentSize = size |
| 58 | + |
| 59 | + // Set initial zoom scale |
| 60 | + scrollView.zoomScale = minScale |
| 61 | + } |
| 62 | + |
| 63 | + return scrollView |
| 64 | + } |
| 65 | + |
| 66 | + func updateUIView(_ scrollView: UIScrollView, context: Context) { |
| 67 | + if let imageView = context.coordinator.imageView as? FLAnimatedImageView, |
| 68 | + let animatedImage = FLAnimatedImage(animatedGIFData: data) { |
| 69 | + imageView.animatedImage = animatedImage |
| 70 | + context.coordinator.updateZoomScaleForSize(scrollView.bounds.size) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + class Coordinator: NSObject, UIScrollViewDelegate { |
| 75 | + let parent: AnimatedImageScrollView |
| 76 | + weak var imageView: FLAnimatedImageView? |
| 77 | + weak var scrollView: UIScrollView? |
| 78 | + |
| 79 | + init(_ parent: AnimatedImageScrollView) { |
| 80 | + self.parent = parent |
| 81 | + } |
| 82 | + |
| 83 | + func viewForZooming(in scrollView: UIScrollView) -> UIView? { |
| 84 | + return imageView |
| 85 | + } |
| 86 | + |
| 87 | + func updateZoomScaleForSize(_ size: CGSize) { |
| 88 | + guard let imageView = imageView, |
| 89 | + let animatedImage = imageView.animatedImage, |
| 90 | + let scrollView = scrollView, |
| 91 | + size.width > 0, |
| 92 | + size.height > 0, |
| 93 | + animatedImage.size.width > 0, |
| 94 | + animatedImage.size.height > 0 else { return } |
| 95 | + |
| 96 | + let widthScale = size.width / animatedImage.size.width |
| 97 | + let heightScale = size.height / animatedImage.size.height |
| 98 | + let minScale = min(widthScale, heightScale) |
| 99 | + |
| 100 | + scrollView.minimumZoomScale = minScale |
| 101 | + scrollView.maximumZoomScale = max(minScale * 5, 5.0) |
| 102 | + } |
| 103 | + |
| 104 | + @objc func handleDoubleTap(_ gesture: UITapGestureRecognizer) { |
| 105 | + guard let scrollView = gesture.view as? UIScrollView else { return } |
| 106 | + |
| 107 | + if scrollView.zoomScale > scrollView.minimumZoomScale { |
| 108 | + scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true) |
| 109 | + } else { |
| 110 | + let point = gesture.location(in: imageView) |
| 111 | + let size = scrollView.bounds.size |
| 112 | + let w = size.width / (scrollView.maximumZoomScale / 5) |
| 113 | + let h = size.height / (scrollView.maximumZoomScale / 5) |
| 114 | + let x = point.x - (w / 2.0) |
| 115 | + let y = point.y - (h / 2.0) |
| 116 | + let rect = CGRect(x: x, y: y, width: w, height: h) |
| 117 | + scrollView.zoom(to: rect, animated: true) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + func scrollViewDidZoom(_ scrollView: UIScrollView) { |
| 122 | + guard let imageView = imageView else { return } |
| 123 | + |
| 124 | + let boundsSize = scrollView.bounds.size |
| 125 | + var frameToCenter = imageView.frame |
| 126 | + |
| 127 | + if frameToCenter.size.width < boundsSize.width { |
| 128 | + frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2 |
| 129 | + } else { |
| 130 | + frameToCenter.origin.x = 0 |
| 131 | + } |
| 132 | + |
| 133 | + if frameToCenter.size.height < boundsSize.height { |
| 134 | + frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2 |
| 135 | + } else { |
| 136 | + frameToCenter.origin.y = 0 |
| 137 | + } |
| 138 | + |
| 139 | + imageView.frame = frameToCenter |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments