Skip to content

Commit

Permalink
Merge pull request #92 from jrsaruo/release-0.1.2
Browse files Browse the repository at this point in the history
Release 0.1.2
  • Loading branch information
jrsaruo authored Dec 23, 2023
2 parents 4b4e5fb + 4f9d9f0 commit de0e1c9
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 31 deletions.
67 changes: 51 additions & 16 deletions Demo/MediaViewerDemo/Samples/Camera/CameraLikeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ final class CameraLikeView: UIView {
return button
}()

private let buttonArea: UIView = {
let view = UIView()
view.backgroundColor = .black.withAlphaComponent(0.1)
return view
}()

private let shutterButtonWidth = 68.0

// MARK: - Initializers

override init(frame: CGRect) {
Expand All @@ -58,42 +66,69 @@ final class CameraLikeView: UIView {

// Subviews
addSubview(previewView)
addSubview(shutterButton)
addSubview(showLibraryButton)
addSubview(toggleTabBarHiddenButton)
addSubview(buttonArea)
buttonArea.addSubview(shutterButton)
buttonArea.addSubview(showLibraryButton)
buttonArea.addSubview(toggleTabBarHiddenButton)

let bottomAreaLayoutGuide = UILayoutGuide()
addLayoutGuide(bottomAreaLayoutGuide)
shutterButton.layer.cornerRadius = shutterButtonWidth / 2

// Layout
showLibraryButton.autoLayout { item in
item.leading.equal(to: layoutMarginsGuide)
item.centerY.equal(to: shutterButton)
item.size.equal(toSquare: 48)
}

toggleTabBarHiddenButton.autoLayout { item in
item.trailing.equal(to: layoutMarginsGuide)
item.centerY.equal(to: shutterButton)
}

switch traitCollection.horizontalSizeClass {
case .unspecified, .compact:
layoutForCompactScreen()
case .regular:
layoutForRegularScreen()
@unknown default:
fatalError()
}
}

private func layoutForCompactScreen() {
previewView.autoLayout { item in
item.top.equal(to: safeAreaLayoutGuide, plus: 44)
item.width.equal(to: item.height, multipliedBy: 3.0 / 4)
item.leadingTrailing.equalToSuperview()
}

bottomAreaLayoutGuide.autoLayout { item in
buttonArea.autoLayout { item in
item.top.equal(to: previewView.bottomAnchor)
item.leadingTrailing.equalToSuperview()
item.bottom.equal(to: safeAreaLayoutGuide)
}

let shutterButtonWidth = 68.0
shutterButton.layer.cornerRadius = shutterButtonWidth / 2
shutterButton.autoLayout { item in
item.center.equal(to: bottomAreaLayoutGuide)
item.center.equalToSuperview()
item.size.equal(toSquare: shutterButtonWidth)
}
}

private func layoutForRegularScreen() {
previewView.autoLayout { item in
item.edges.equalToSuperview()
}

showLibraryButton.autoLayout { item in
item.leading.equal(to: layoutMarginsGuide)
item.centerY.equal(to: bottomAreaLayoutGuide)
item.size.equal(toSquare: 48)
buttonArea.autoLayout { item in
item.leadingTrailing.equalToSuperview()
item.bottom.equalToSuperview()
}

toggleTabBarHiddenButton.autoLayout { item in
item.trailing.equal(to: layoutMarginsGuide)
item.centerY.equal(to: bottomAreaLayoutGuide)
shutterButton.autoLayout { item in
item.centerX.equalToSuperview()
item.top.equalToSuperview(plus: 16)
item.bottom.equal(to: safeAreaLayoutGuide, plus: -16)
item.size.equal(toSquare: shutterButtonWidth)
}
}
}
Expand Down
27 changes: 24 additions & 3 deletions Demo/MediaViewerDemo/Samples/Grid/ImageGridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ final class ImageGridView: UIView {

let collectionView: UICollectionView = {
let layout = UICollectionViewCompositionalLayout { _, layoutEnvironment in
let columnCount = 3
let itemSpacing = 2.0
let minimumItemWidth: CGFloat
let itemSpacing: CGFloat
let contentInsetsReference: UIContentInsetsReference
switch layoutEnvironment.traitCollection.horizontalSizeClass {
case .unspecified, .compact:
minimumItemWidth = 130
itemSpacing = 2
contentInsetsReference = .automatic
case .regular:
minimumItemWidth = 160
itemSpacing = 16
contentInsetsReference = .layoutMargins
@unknown default:
fatalError()
}

let effectiveFullWidth = layoutEnvironment.container.effectiveContentSize.width
let columnCount = Int(effectiveFullWidth / minimumItemWidth)
let totalSpacing = itemSpacing * CGFloat(columnCount - 1)
let estimatedItemWidth = (effectiveFullWidth - totalSpacing) / CGFloat(columnCount)
let group = NSCollectionLayoutGroup.horizontal(
Expand All @@ -33,10 +47,16 @@ final class ImageGridView: UIView {

let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = itemSpacing
section.contentInsetsReference = contentInsetsReference
return section
}

return UICollectionView(frame: .zero, collectionViewLayout: layout)
let collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: layout
)
collectionView.preservesSuperviewLayoutMargins = true
return collectionView
}()

// MARK: - Initializers
Expand All @@ -52,6 +72,7 @@ final class ImageGridView: UIView {
}

private func setUpViews() {
preservesSuperviewLayoutMargins = true
backgroundColor = .systemBackground

// Subviews
Expand Down
19 changes: 19 additions & 0 deletions Demo/MediaViewerDemo/Samples/Grid/SyncImagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ final class SyncImagesViewController: UIViewController {
}
)

private lazy var toggleToolbarButton = UIBarButtonItem(
title: "Toggle Toolbar",
primaryAction: .init { [weak self] _ in
guard let self, let navigationController else { return }
navigationController.setToolbarHidden(
!navigationController.isToolbarHidden,
animated: true
)
}
)

// MARK: - Lifecycle

override func loadView() {
Expand All @@ -79,6 +90,14 @@ final class SyncImagesViewController: UIViewController {
navigationItem.title = "Sync Sample"
navigationItem.backButtonDisplayMode = .minimal
navigationItem.leftBarButtonItem = refreshButton
navigationItem.rightBarButtonItem = toggleToolbarButton

// Toolbar
toolbarItems = [
.flexibleSpace(),
.init(title: "Toolbar"),
.flexibleSpace()
]

// Subviews
imageGridView.collectionView.refreshControl = .init(
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ See demo for more detailed usage.
To use the `MediaViewer` library in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:

```swift
.package(url: "https://github.com/jrsaruo/MediaViewer", from: "0.1.1"),
.package(url: "https://github.com/jrsaruo/MediaViewer", from: "0.1.2"),
```

and add `MediaViewer` as a dependency for your target:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ extension MediaViewerInteractivePopTransition: UIViewControllerInteractiveTransi
: 1

// [Workaround] Prevent toVC.toolbarItems from showing up during transition.
toVC.toolbarItems = nil
if mediaViewer.toolbarHiddenBackup {
toVC.toolbarItems = nil
}

/*
* [Workaround]
Expand All @@ -152,10 +154,10 @@ extension MediaViewerInteractivePopTransition: UIViewControllerInteractiveTransi
var viewsToFadeDuringTransition = mediaViewer.subviewsToFadeDuringTransition
let isTabBarHidden = tabBar?.isHidden ?? true
if isTabBarHidden {
viewsToFadeDuringTransition.append(contentsOf: [
toolbar,
mediaViewer.pageControlToolbar
])
if mediaViewer.toolbarHiddenBackup {
viewsToFadeDuringTransition.append(toolbar)
}
viewsToFadeDuringTransition.append(mediaViewer.pageControlToolbar)
}

mediaViewer.willStartInteractivePopTransition()
Expand Down Expand Up @@ -186,6 +188,15 @@ extension MediaViewerInteractivePopTransition: UIViewControllerInteractiveTransi
width: pageControlToolbarFrame.width,
height: 0
)

/*
* [Workaround]
* If the tabBar becomes visible and the toolbar remains visible,
* move it manually because repositioning is not animated.
*/
if !mediaViewer.toolbarHiddenBackup, let tabBar = self.tabBar {
toolbar.frame.origin.y = tabBar.frame.origin.y - toolbar.bounds.height
}
}
}

Expand Down
36 changes: 30 additions & 6 deletions Sources/MediaViewer/Transitions/MediaViewerTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ final class MediaViewerTransition: NSObject, UIViewControllerAnimatedTransitioni
}

var viewsToFadeDuringTransition = mediaViewer.subviewsToFadeDuringTransition
viewsToFadeDuringTransition.append(toolbar)
if wasTabBarHidden {
viewsToFadeDuringTransition.append(mediaViewer.pageControlToolbar)
}
if mediaViewer.toolbarHiddenBackup {
viewsToFadeDuringTransition.append(toolbar)
}
for view in viewsToFadeDuringTransition {
view.alpha = 0
}
Expand Down Expand Up @@ -188,6 +190,17 @@ final class MediaViewerTransition: NSObject, UIViewControllerAnimatedTransitioni
currentPageImageView.contentMode = sourceView.contentMode
}
currentPageImageView.layer.masksToBounds = true

/*
* [Workaround]
* If the tabBar becomes hidden and the toolbar remains visible,
* move it manually because repositioning is not animated.
*/
if !mediaViewer.toolbarHiddenBackup,
let tabBar,
tabBarHiddenBackup! {
toolbar.frame.origin.y = tabBar.frame.origin.y
}
}
animator.addCompletion { position in
defer { transitionContext.completeTransition() }
Expand Down Expand Up @@ -243,7 +256,9 @@ final class MediaViewerTransition: NSObject, UIViewControllerAnimatedTransitioni
assert(toolbar.layer.animationKeys() == nil)

// [Workaround] Prevent toVC.toolbarItems from showing up during transition.
toVC.toolbarItems = nil
if mediaViewer.toolbarHiddenBackup {
toVC.toolbarItems = nil
}

/*
* [Workaround]
Expand Down Expand Up @@ -282,10 +297,10 @@ final class MediaViewerTransition: NSObject, UIViewControllerAnimatedTransitioni
let tabBar = toVC.tabBarController?.tabBar
let isTabBarHidden = tabBar?.isHidden ?? true
if isTabBarHidden {
viewsToFadeDuringTransition.append(contentsOf: [
toolbar,
mediaViewer.pageControlToolbar
])
if mediaViewer.toolbarHiddenBackup {
viewsToFadeDuringTransition.append(toolbar)
}
viewsToFadeDuringTransition.append(mediaViewer.pageControlToolbar)
}

// MARK: Animation
Expand All @@ -307,6 +322,15 @@ final class MediaViewerTransition: NSObject, UIViewControllerAnimatedTransitioni
currentPageImageView.alpha = 0
}
currentPageImageView.clipsToBounds = true // TODO: Change according to the source configuration

/*
* [Workaround]
* If the tabBar becomes visible and the toolbar remains visible,
* move it manually because repositioning is not animated.
*/
if !mediaViewer.toolbarHiddenBackup, let tabBar {
toolbar.frame.origin.y = tabBar.frame.origin.y - toolbar.bounds.height
}
}

// Customize the tabBar animation
Expand Down

0 comments on commit de0e1c9

Please sign in to comment.