Skip to content

Commit

Permalink
chore: Address review feedback
Browse files Browse the repository at this point in the history
- Add documentation for assetPreviewFooterView
- Rename adjustViewFramesIfNeeded -> adjustViewFrameIfNeeded
- Rename v -> pannedView in PanGestureHelper
  • Loading branch information
iirovi-rs committed Apr 16, 2024
1 parent 9aea46b commit 3c646ce
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ public struct YPConfigLibrary {
/// Allow selection of both photo and video from the photo gallery.
public var allowPhotoAndVideoSelection : Bool = false

/// A view you set here will be shown underneath the asset preview view in the library screen.
/// The view will be automatically laid out to fill the screen horizontally, but it should determine its own height.
public var assetPreviewFooterView: UIView?
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Filters/Photo/YPCoverImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public class YPCoverImageView: YPAdjustableView {
// Ensure necessary properties are available
guard let cropRect = cropRect else { return }
if let asset = asset {
adjustViewFramesIfNeeded(cropRect: cropRect, asset: asset, targetAspectRatio: targetAspectRatio)
adjustViewFrameIfNeeded(cropRect: cropRect, asset: asset, targetAspectRatio: targetAspectRatio)
} else if let videoUrl = videoUrl {
let videoAsset = AVAsset(url: videoUrl)
guard let track = videoAsset.tracks(withMediaType: AVMediaType.video).first else { return }
let size = track.naturalSize.applying(track.preferredTransform)
adjustViewFramesIfNeeded(cropRect: cropRect, assetSize: size, targetAspectRatio: targetAspectRatio)
adjustViewFrameIfNeeded(cropRect: cropRect, assetSize: size, targetAspectRatio: targetAspectRatio)
}
}

Expand Down
7 changes: 4 additions & 3 deletions Source/Filters/Video/YPVideoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ public class YPVideoView: YPAdjustableView {

updateViewFrameAction = { [weak self] frame in
self?.playerView.layer.frame = frame
self?.playerView.playerLayer.speed = 999 // disable the "zoom in" animation
self?.playerView.playerLayer.speed = 999 // disable the "zoom in" animation by making the animation speed "fast enough"
self?.playerView.playerLayer.videoGravity = .resizeAspectFill
}

// Ensure necessary properties are available
guard let cropRect = cropRect else { return }
if let asset = asset {
adjustViewFramesIfNeeded(cropRect: cropRect, asset: asset, targetAspectRatio: targetAspectRatio)
adjustViewFrameIfNeeded(cropRect: cropRect, asset: asset, targetAspectRatio: targetAspectRatio)
} else if let videoUrl = videoUrl {
let videoAsset = AVAsset(url: videoUrl)
guard let track = videoAsset.tracks(withMediaType: AVMediaType.video).first else { return }
let size = track.naturalSize.applying(track.preferredTransform)
adjustViewFramesIfNeeded(cropRect: cropRect, assetSize: size, targetAspectRatio: targetAspectRatio)
adjustViewFrameIfNeeded(cropRect: cropRect, assetSize: size, targetAspectRatio: targetAspectRatio)
}
}

Expand Down Expand Up @@ -127,6 +127,7 @@ extension YPVideoView {
videoUrl = url
player = AVPlayer(url: url)
case let playerItem as AVPlayerItem:
videoUrl = (playerItem.asset as? AVURLAsset)?.url
player = AVPlayer(playerItem: playerItem)
default:
return
Expand Down
4 changes: 2 additions & 2 deletions Source/Helpers/YPAdjustableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class YPAdjustableView: UIView {

public var updateViewFrameAction: ((CGRect) -> Void)?

public func adjustViewFramesIfNeeded(cropRect: CGRect, asset: PHAsset, targetAspectRatio: CGFloat?) {
public func adjustViewFrameIfNeeded(cropRect: CGRect, asset: PHAsset, targetAspectRatio: CGFloat?) {
let assetSize = CGSize(width: CGFloat(asset.pixelWidth), height: CGFloat(asset.pixelHeight))

if let aspectRatio = targetAspectRatio, aspectRatio != assetSize.width / assetSize.height {
Expand All @@ -24,7 +24,7 @@ public class YPAdjustableView: UIView {
}
}

public func adjustViewFramesIfNeeded(cropRect: CGRect, assetSize: CGSize, targetAspectRatio: CGFloat?) {
public func adjustViewFrameIfNeeded(cropRect: CGRect, assetSize: CGSize, targetAspectRatio: CGFloat?) {
if let aspectRatio = targetAspectRatio, aspectRatio != assetSize.width / assetSize.height {
adjustViewFrameForAspectRatio(cropRect: cropRect, aspectRatio: aspectRatio, assetSize: assetSize)
} else {
Expand Down
42 changes: 21 additions & 21 deletions Source/Pages/Gallery/YPLibraryVC+PanGesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {

var v: YPLibraryViewPanning!
var pannedView: YPLibraryViewPanning!
var targetView: UIView!
private let assetViewContainerOriginalConstraintTop: CGFloat = 0
private var dragDirection = YPDragDirection.up
Expand All @@ -24,11 +24,11 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
// The height constraint of the view with main selected image
var topHeight: CGFloat {
get {
return v.imageContainerConstraintTop?.constant ?? 0
return pannedView.imageContainerConstraintTop?.constant ?? 0
}
set {
if newValue >= v.imageScrollViewMinimalVisibleHeight - v.imageContainerFrame.size.height {
v.imageContainerConstraintTop?.constant = newValue
if newValue >= pannedView.imageScrollViewMinimalVisibleHeight - pannedView.imageContainerFrame.size.height {
pannedView.imageContainerConstraintTop?.constant = newValue
}
}
}
Expand All @@ -39,15 +39,15 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
set {
if newValue != isImageShown {
self._isImageShown = newValue
v.assetViewContainerIsShown = newValue
pannedView.assetViewContainerIsShown = newValue
// Update imageCropContainer
v.imageScrollViewScrollEnabled = isImageShown
pannedView.imageScrollViewScrollEnabled = isImageShown
}
}
}

public func registerForPanGesture(on view: YPLibraryViewPanning) {
v = view
pannedView = view
targetView = view.targetView
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panned(_:)))
panGesture.delegate = self
Expand All @@ -62,7 +62,7 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
}

public func animateUp() {
topHeight = v.imageScrollViewMinimalVisibleHeight - v.imageContainerFrame.size.height
topHeight = pannedView.imageScrollViewMinimalVisibleHeight - pannedView.imageContainerFrame.size.height
animateView()
dragDirection = .down
}
Expand All @@ -72,8 +72,8 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
delay: 0.0,
options: [.curveEaseInOut, .beginFromCurrentState],
animations: {
self.v.updateImageContainerLayout()
self.v.targetView.layoutIfNeeded()
self.pannedView.updateImageContainerLayout()
self.pannedView.targetView.layoutIfNeeded()
},
completion: nil)
}
Expand All @@ -87,7 +87,7 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
let p = gestureRecognizer.location(ofTouch: 0, in: targetView)
// Desactivate pan on image when it is shown.
if isImageShown {
if p.y < v.imageContainerFrame.height {
if p.y < pannedView.imageContainerFrame.height {
return false
}
}
Expand All @@ -97,7 +97,7 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
@objc
func panned(_ sender: UIPanGestureRecognizer) {

let containerHeight = v.imageContainerFrame.size.height + (v.collectionViewFrame.minY - v.imageContainerFrame.maxY)
let containerHeight = pannedView.imageContainerFrame.size.height + (pannedView.collectionViewFrame.minY - pannedView.imageContainerFrame.maxY)
let currentPos = sender.location(in: targetView)
let overYLimitToStartMovingUp = currentPos.y * 1.4 < cropBottomY
switch sender.state {
Expand All @@ -106,13 +106,13 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
let loc = sender.location(in: view)
let subview = view?.hitTest(loc, with: nil)

if subview == v.imageContainerView
if subview == pannedView.imageContainerView
&& topHeight == assetViewContainerOriginalConstraintTop {
return
}

dragStartPos = sender.location(in: view)
cropBottomY = v.collectionViewFrame.minY
cropBottomY = pannedView.collectionViewFrame.minY

// Move
if dragDirection == .stop {
Expand All @@ -125,28 +125,28 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
if dragDirection == .down && dragStartPos.y > cropBottomY {
dragDirection = .stop
}
if (dragDirection == .up || dragDirection == .down) && currentPos.y < v.collectionViewFrame.minY {
dragDiff = currentPos.y - v.collectionViewFrame.minY
if (dragDirection == .up || dragDirection == .down) && currentPos.y < pannedView.collectionViewFrame.minY {
dragDiff = currentPos.y - pannedView.collectionViewFrame.minY
}

case .changed:
switch dragDirection {
case .up:
let diff = v.collectionViewFrame.minY - currentPos.y
let diff = pannedView.collectionViewFrame.minY - currentPos.y
if diff > 0 {
topHeight =
min(assetViewContainerOriginalConstraintTop,
max(v.imageScrollViewMinimalVisibleHeight - containerHeight,
max(pannedView.imageScrollViewMinimalVisibleHeight - containerHeight,
(currentPos.y - dragDiff) - containerHeight))
}
case .down:
topHeight = min(assetViewContainerOriginalConstraintTop, currentPos.y - dragDiff - containerHeight)
case .scroll:
let newTopHeightValue = v.imageScrollViewMinimalVisibleHeight - v.imageContainerFrame.size.height
+ (currentPos.y - v.collectionViewContentOffset.y) - imaginaryCollectionViewOffsetStartPosY
let newTopHeightValue = pannedView.imageScrollViewMinimalVisibleHeight - pannedView.imageContainerFrame.size.height
+ (currentPos.y - pannedView.collectionViewContentOffset.y) - imaginaryCollectionViewOffsetStartPosY
topHeight = newTopHeightValue
case .stop:
if topHeight != assetViewContainerOriginalConstraintTop && v.collectionViewContentOffset.y < 0 {
if topHeight != assetViewContainerOriginalConstraintTop && pannedView.collectionViewContentOffset.y < 0 {
dragDirection = .scroll
imaginaryCollectionViewOffsetStartPosY = currentPos.y
}
Expand Down

0 comments on commit 3c646ce

Please sign in to comment.