Skip to content

Commit

Permalink
fix: Wait until scrollView animation stops before navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
iirovi-rs committed Mar 15, 2024
1 parent 6733b5b commit b0f7bdc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Source/Pages/Gallery/YPAssetZoomableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ final class YPAssetZoomableView: UIScrollView {

private let minimumTreshold: CGFloat = 0.001

public var isAnimating: Bool = false

// Image view of the asset for convenience. Can be video preview image view or photo image view.
public var assetImageView: UIImageView {
return isVideoMode ? videoView.previewImageView : photoImageView
Expand Down Expand Up @@ -401,11 +403,19 @@ extension YPAssetZoomableView: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return isVideoMode ? videoView : photoImageView
}


func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
isAnimating = true
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidZoom()
}


func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isAnimating = true
}

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
guard let view = view, view == photoImageView || view == videoView else { return }

Expand All @@ -416,13 +426,16 @@ extension YPAssetZoomableView: UIScrollViewDelegate {

zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidEndZooming()
cropAreaDidChange()
isAnimating = false
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
cropAreaDidChange()
isAnimating = decelerate
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
cropAreaDidChange()
isAnimating = false
}
}
4 changes: 4 additions & 0 deletions Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public final class YPLibraryVC: UIViewController, YPPermissionCheckable {
internal var isInitialized = false
var disableAutomaticCellSelection = false

public var isAnimating: Bool {
v.assetZoomableView.isAnimating
}

// MARK: - Init

public override func loadView() {
Expand Down
19 changes: 18 additions & 1 deletion Source/YPPickerVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ open class YPPickerVC: YPBottomPager, YPBottomPagerDelegate {
@objc
func done() {
guard let libraryVC = libraryVC else { ypLog("YPLibraryVC deallocated"); return }
if libraryVC.isAnimating {
self.navigationItem.rightBarButtonItem = YPLoaders.defaultLoader
retryDoneUntilAnimatingStops(retryCount: 0)
return
}
didTapNext?()

if mode == .library {
Expand All @@ -350,7 +355,19 @@ open class YPPickerVC: YPBottomPager, YPBottomPagerDelegate {
})
}
}


let maxRetryCount = 6
func retryDoneUntilAnimatingStops(retryCount: Int = 0) {
guard let libraryVC = libraryVC else { ypLog("YPLibraryVC deallocated"); return }
if libraryVC.isAnimating, retryCount < maxRetryCount {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self ] in
self?.retryDoneUntilAnimatingStops(retryCount: retryCount + 1)
}
} else {
done()
}
}

func stopAll() {
libraryVC?.v.assetZoomableView.videoView.deallocate()
videoVC?.stopCamera()
Expand Down

0 comments on commit b0f7bdc

Please sign in to comment.