Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back (pop) custom animation #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions custom-transitions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -102,6 +102,7 @@
BC78E7A026163BB500371773 /* Products */,
);
sourceTree = "<group>";
usesTabs = 0;
};
BC78E7A026163BB500371773 /* Products */ = {
isa = PBXGroup;
Expand Down Expand Up @@ -241,8 +242,9 @@
BC78E79726163BB500371773 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1230;
LastUpgradeCheck = 1230;
LastUpgradeCheck = 1540;
TargetAttributes = {
BC78E79E26163BB500371773 = {
CreatedOnToolsVersion = 12.3;
Expand Down Expand Up @@ -370,6 +372,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -431,6 +434,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
58 changes: 49 additions & 9 deletions custom-transitions/Transitions/TransitionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning {
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
duration
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
Expand All @@ -36,14 +36,14 @@ final class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning {
// MARK: - UINavigationControllerDelegate

extension TransitionManager: UINavigationControllerDelegate {
func navigationController(
_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationController.Operation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

self.operation = operation

if operation == .push {
if operation == .push || operation == .pop {
return self
}

Expand Down Expand Up @@ -71,7 +71,7 @@ private extension TransitionManager {
let albumsViewController = toViewController as? AlbumsViewController
else { return }

dismissViewController(detailsViewController, to: albumsViewController)
dismissViewController(detailsViewController, to: albumsViewController, with: context)

default:
break
Expand All @@ -84,7 +84,7 @@ private extension TransitionManager {
let albumCell = fromViewController.currentCell,
let albumCoverImageView = fromViewController.currentCell?.albumCoverImageView,
let albumDetailHeaderView = toViewController.headerView
else { return}
else { return }

toViewController.view.layoutIfNeeded()

Expand Down Expand Up @@ -125,7 +125,47 @@ private extension TransitionManager {
animator.startAnimation()
}

func dismissViewController(_ fromViewController: AlbumDetailViewController, to toViewController: AlbumsViewController) {
func dismissViewController(_ fromViewController: AlbumDetailViewController, to toViewController: AlbumsViewController, with context: UIViewControllerContextTransitioning) {
guard
let albumCell = toViewController.currentCell,
let albumCoverImageView = toViewController.currentCell?.albumCoverImageView,
let albumDetailHeaderView = fromViewController.headerView,
let albumDetailRootView = fromViewController.view
else { return }

toViewController.view.layoutIfNeeded()

let containerView = context.containerView

let snapshotContentView = UIView()
snapshotContentView.backgroundColor = albumDetailRootView.backgroundColor
snapshotContentView.frame = albumDetailRootView.frame
snapshotContentView.layer.cornerRadius = albumCell.contentView.layer.cornerRadius

let snapshotAlbumCoverImageView = UIImageView()
snapshotAlbumCoverImageView.clipsToBounds = true
snapshotAlbumCoverImageView.contentMode = albumDetailHeaderView.contentMode
snapshotAlbumCoverImageView.image = albumDetailHeaderView.albumCoverImageView.image
snapshotAlbumCoverImageView.layer.cornerRadius = albumDetailHeaderView.layer.cornerRadius
snapshotAlbumCoverImageView.frame = containerView.convert(albumDetailHeaderView.albumCoverImageView.frame, from: albumDetailHeaderView)

containerView.addSubview(toViewController.view)
containerView.addSubview(snapshotContentView)
containerView.addSubview(snapshotAlbumCoverImageView)

let animator = UIViewPropertyAnimator(duration: duration, curve: .easeInOut) {
snapshotContentView.frame = containerView.convert(albumCell.contentView.frame, from: albumCell)
snapshotContentView.backgroundColor = albumCell.contentView.backgroundColor
snapshotAlbumCoverImageView.frame = containerView.convert(albumCoverImageView.frame, from: albumCell)
snapshotAlbumCoverImageView.layer.cornerRadius = albumCoverImageView.layer.cornerRadius
}

animator.addCompletion { position in
snapshotAlbumCoverImageView.removeFromSuperview()
snapshotContentView.removeFromSuperview()
context.completeTransition(position == .end)
}

animator.startAnimation()
}
}
2 changes: 1 addition & 1 deletion custom-transitions/View/AlbumDetailHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

protocol AlbumDetailHeaderViewDelegate: class {
protocol AlbumDetailHeaderViewDelegate: AnyObject {
func closeButtonDidTap()
}

Expand Down
3 changes: 1 addition & 2 deletions custom-transitions/ViewController/AlbumsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class AlbumsViewController: UIViewController {
}

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)
}

}

// MARK: - Setups
Expand Down