|
| 1 | +// |
| 2 | +// CustomScrollView.swift |
| 3 | +// ArticleFeature |
| 4 | +// |
| 5 | +// Created by Виталий Канин on 11.03.2025. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import Models |
| 10 | + |
| 11 | +struct CustomScrollView: UIViewRepresentable { |
| 12 | + |
| 13 | + let imageElement: [URL] |
| 14 | + @Binding var selectedIndex: Int |
| 15 | + @Binding var isZooming: Bool |
| 16 | + @Binding var isTouched: Bool |
| 17 | + @Binding var backgroundOpacity: Double |
| 18 | + var onClose: (() -> Void)? |
| 19 | + |
| 20 | + func makeUIView(context: Context) -> UICollectionView { |
| 21 | + |
| 22 | + let layout = UICollectionViewFlowLayout() |
| 23 | + layout.scrollDirection = .horizontal |
| 24 | + layout.minimumLineSpacing = 0 |
| 25 | + layout.itemSize = UIScreen.main.bounds.size |
| 26 | + |
| 27 | + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) |
| 28 | + collectionView.isPagingEnabled = true |
| 29 | + collectionView.showsHorizontalScrollIndicator = false |
| 30 | + collectionView.showsVerticalScrollIndicator = false |
| 31 | + collectionView.backgroundColor = .clear |
| 32 | + collectionView.dataSource = context.coordinator |
| 33 | + collectionView.delegate = context.coordinator |
| 34 | + collectionView.backgroundColor = .black |
| 35 | + collectionView.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "ImageCollectionViewCell") |
| 36 | + |
| 37 | + let panGesture = UIPanGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleVerticalSwipe(_:))) |
| 38 | + panGesture.delegate = context.coordinator as any UIGestureRecognizerDelegate |
| 39 | + collectionView.addGestureRecognizer(panGesture) |
| 40 | + |
| 41 | + return collectionView |
| 42 | + } |
| 43 | + |
| 44 | + func updateUIView(_ uiView: UICollectionView, context: Context) { |
| 45 | + let indexPath = IndexPath(item: selectedIndex, section: 0) |
| 46 | + Task { @MainActor in |
| 47 | + uiView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func makeCoordinator() -> Coordinator { |
| 52 | + Coordinator(self) |
| 53 | + } |
| 54 | + |
| 55 | + class Coordinator: NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate{ |
| 56 | + var parent: CustomScrollView |
| 57 | + private var initialTouchPoint: CGPoint = .zero |
| 58 | + private var firstSwipeDirection: SwipeDirection = .none |
| 59 | + |
| 60 | + enum SwipeDirection { |
| 61 | + case horizontal |
| 62 | + case vertical |
| 63 | + case none |
| 64 | + } |
| 65 | + |
| 66 | + init(_ parent: CustomScrollView) { |
| 67 | + self.parent = parent |
| 68 | + } |
| 69 | + |
| 70 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 71 | + return parent.imageElement.count |
| 72 | + } |
| 73 | + |
| 74 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 75 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell |
| 76 | + cell.setImage(url: parent.imageElement[indexPath.item]) |
| 77 | + |
| 78 | + cell.onZoom = { isZooming in |
| 79 | + Task { @MainActor in |
| 80 | + self.parent.isZooming = isZooming |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + cell.onToolBar = { |
| 85 | + Task { @MainActor in |
| 86 | + self.parent.isTouched.toggle() |
| 87 | + } |
| 88 | + } |
| 89 | + return cell |
| 90 | + } |
| 91 | + |
| 92 | + func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { |
| 93 | + let pageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width) |
| 94 | + self.parent.selectedIndex = pageIndex |
| 95 | + |
| 96 | + // We always have a last gesture |
| 97 | + if let gestureRecognizers = scrollView.gestureRecognizers, let lastGesture = gestureRecognizers.last { |
| 98 | + lastGesture.isEnabled = true |
| 99 | + } |
| 100 | + |
| 101 | + firstSwipeDirection = .none |
| 102 | + } |
| 103 | + |
| 104 | + @objc func handleVerticalSwipe(_ gesture: UIPanGestureRecognizer) { |
| 105 | + guard let collectionView = gesture.view as? UICollectionView else { return } |
| 106 | + guard let visibleCell = collectionView.visibleCells.first as? ImageCollectionViewCell else { return } |
| 107 | + let translation = gesture.translation(in: gesture.view?.superview) |
| 108 | + if parent.isZooming { return } |
| 109 | + |
| 110 | + switch gesture.state { |
| 111 | + case .began: |
| 112 | + initialTouchPoint = gesture.location(in: gesture.view?.superview) |
| 113 | + case .changed: |
| 114 | + if abs(translation.y) > abs(translation.x) && firstSwipeDirection == .vertical { |
| 115 | + collectionView.isScrollEnabled = false |
| 116 | + visibleCell.transform = CGAffineTransform(translationX: 0, y: translation.y) |
| 117 | + self.parent.backgroundOpacity = max(0.1, 1 - Double(abs(translation.y * 5) / 700)) |
| 118 | + collectionView.layer.opacity = max(0.1, 1 - Float(abs(translation.y * 2.5) / 700)) |
| 119 | + } |
| 120 | + case .ended, .cancelled: |
| 121 | + if abs(translation.y) > 150 { |
| 122 | + parent.onClose?() |
| 123 | + UIView.animate(withDuration: 0.6, |
| 124 | + delay: 0, |
| 125 | + usingSpringWithDamping: 0.8, |
| 126 | + initialSpringVelocity: 0.3, |
| 127 | + options: .curveEaseInOut, |
| 128 | + animations: { |
| 129 | + self.parent.backgroundOpacity = 0.0 |
| 130 | + collectionView.layer.opacity = 0.0 |
| 131 | + }) |
| 132 | + } else { |
| 133 | + UIView.animate(withDuration: 0.6, |
| 134 | + delay: 0, |
| 135 | + usingSpringWithDamping: 0.8, |
| 136 | + initialSpringVelocity: 0.3, |
| 137 | + options: .curveEaseInOut, |
| 138 | + animations: { |
| 139 | + visibleCell.transform = CGAffineTransform(translationX: 0, y: 0) |
| 140 | + self.parent.backgroundOpacity = 1.0 |
| 141 | + collectionView.layer.opacity = 1.0 |
| 142 | + }) |
| 143 | + } |
| 144 | + firstSwipeDirection = .none |
| 145 | + collectionView.isScrollEnabled = true |
| 146 | + case .failed, .possible: |
| 147 | + break |
| 148 | + @unknown default: |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { |
| 154 | + guard let collection = otherGestureRecognizer.view as? UICollectionView else { |
| 155 | + return false |
| 156 | + } |
| 157 | + |
| 158 | + if parent.imageElement.count == 1 { |
| 159 | + firstSwipeDirection = .vertical |
| 160 | + return true |
| 161 | + } |
| 162 | + |
| 163 | + if let panGesture = gestureRecognizer as? UIPanGestureRecognizer { |
| 164 | + let velocity = panGesture.velocity(in: panGesture.view) |
| 165 | + if firstSwipeDirection == .none { |
| 166 | + if abs(velocity.x) > abs(velocity.y) { |
| 167 | + firstSwipeDirection = .horizontal |
| 168 | + collection.isScrollEnabled = true |
| 169 | + panGesture.isEnabled = false // |
| 170 | + } else if abs(velocity.x) < abs(velocity.y) { |
| 171 | + firstSwipeDirection = .vertical |
| 172 | + otherGestureRecognizer.isEnabled = true |
| 173 | + collection.isScrollEnabled = false |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return true |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments