Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Add multi-transition support. (#40)
Browse files Browse the repository at this point in the history
Closes #31.

This is a breaking change due to removal of APIs and changing of nullability annotations on existing APIs.

This PR allows a client to associate multiple `Transition` instances with a single view controller transition. When a transition is initiated, each `Transition` instance will be provided the same transition context.

This allows us to build smaller transition types, such as "FadeTransition" or "SlideTransition" which each accept a target view. These transitions can then be combined to create a "slide and fade in transition" like so:

```swift
modalViewController.transitionController.transitions = [
  FadeTransition(target: .foreView),
  SlideUpTransition(target: .foreView)
]
```

Some open questions this change has introduced:

- How does view duplication work across transitions? Ideally if a view were duplicated by one transition it would not be re-duplicated by another, but ensuring that all transitions access views in a safe manner would require adoption of a convention, e.g. `context.resolvedView(for: view)`, which would be routed through a shared view duplicator.
- Who is responsible for associating transitions with a given view controller transition? There are potentially three actors who might be interested in associating Interaction instances. This topic is discussed in #39.
- How might a client easily build a transition that is composed of other transitions? This may warrant a follow-up feature change, possibly with the introduction of a `TransitionWithChildTransitions` protocol that allows the parent transition to return an array of child transitions.
  • Loading branch information
jverkoey authored Sep 13, 2017
1 parent 74c1655 commit 8653958
Show file tree
Hide file tree
Showing 21 changed files with 735 additions and 399 deletions.
11 changes: 4 additions & 7 deletions examples/ContextualExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class ContextualExampleViewController: ExampleViewController {
// Note that in this example we're populating the contextual transition with the tapped view.
// Our rudimentary transition will animate the context view to the center of the screen from its
// current location.
controller.transitionController.transition = ContextualTransition(contextView: tapGesture.view!)
controller.transitionController.transitions = [
FadeTransition(target: .foreView),
ContextualTransition(contextView: tapGesture.view!)
]

present(controller, animated: true)
}
Expand Down Expand Up @@ -97,12 +100,6 @@ private class ContextualTransition: NSObject, Transition {
context.transitionDidEnd()
}

let fadeIn = CABasicAnimation(keyPath: "opacity")
fadeIn.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
fadeIn.fromValue = 0
fadeIn.toValue = 1
addAnimationToLayer(fadeIn, context.foreViewController.view.layer)

// We use a snapshot view to accomplish two things:
// 1) To not affect the context view's state.
// 2) To allow our context view to appear in front of the fore view controller's view.
Expand Down
8 changes: 4 additions & 4 deletions examples/CustomPresentationExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ final class VerticalSheetTransition: NSObject, Transition {
}
}

extension VerticalSheetTransition: TransitionWithPresentation, TransitionWithFallback {
extension VerticalSheetTransition: TransitionWithPresentation, TransitionWithFeasibility {

// We customize the transition going forward but fall back to UIKit for dismissal. Our
// presentation controller will govern both of these transitions.
func fallbackTransition(with context: TransitionContext) -> Transition? {
return context.direction == .forward ? self : nil
func canPerformTransition(with context: TransitionContext) -> Bool {
return context.direction == .forward
}

// This method is invoked when we assign the transition to the transition controller. The result
Expand Down Expand Up @@ -165,7 +165,7 @@ extension CustomPresentationExampleViewController {
extension CustomPresentationExampleViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let modal = ModalViewController()
modal.transitionController.transition = transitions[indexPath.row].transition
modal.transitionController.transitions = [transitions[indexPath.row].transition]
showDetailViewController(modal, sender: self)
}
}
4 changes: 2 additions & 2 deletions examples/FadeExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ - (void)didTap {

// The transition controller is an associated object on all UIViewController instances that
// allows you to customize the way the view controller is presented. The primary API on the
// controller that you'll make use of is the `transition` property. Setting this property will
// controller that you'll make use of is the `transitions` property. Setting this property will
// dictate how the view controller is presented. For this example we've built a custom
// FadeTransition, so we'll make use of that now:
viewController.mdm_transitionController.transition = [[FadeTransition alloc] init];
viewController.mdm_transitionController.transitions = @[[[FadeTransition alloc] init]];

// Note that once we assign the transition object to the view controller, the transition will
// govern all subsequent presentations and dismissals of that view controller instance. If we
Expand Down
2 changes: 1 addition & 1 deletion examples/FadeExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FadeExampleViewController: ExampleViewController {
// controller that you'll make use of is the `transition` property. Setting this property will
// dictate how the view controller is presented. For this example we've built a custom
// FadeTransition, so we'll make use of that now:
modalViewController.transitionController.transition = FadeTransition()
modalViewController.transitionController.transitions = [FadeTransition(target: .foreView)]

// Note that once we assign the transition object to the view controller, the transition will
// govern all subsequent presentations and dismissals of that view controller instance. If we
Expand Down
2 changes: 1 addition & 1 deletion examples/MenuExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MenuExampleViewController: ExampleViewController {

func didTap() {
let modalViewController = ModalViewController()
modalViewController.transitionController.transition = MenuTransition()
modalViewController.transitionController.transitions = [MenuTransition()]
present(modalViewController, animated: true)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/NavControllerFadeExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NavControllerFadeExampleViewController: ExampleViewController {
// controller that you'll make use of is the `transition` property. Setting this property will
// dictate how the view controller is presented. For this example we've built a custom
// FadeTransition, so we'll make use of that now:
modalViewController.transitionController.transition = FadeTransition()
modalViewController.transitionController.transitions = [FadeTransition(target: .foreView)]

cachedNavDelegate = navigationController?.delegate

Expand Down
126 changes: 28 additions & 98 deletions examples/PhotoAlbumExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private class PhotoCollectionViewCell: UICollectionViewCell {
}
}

public class PhotoAlbumExampleViewController: UICollectionViewController, PhotoAlbumTransitionDelegate {
public class PhotoAlbumExampleViewController: UICollectionViewController, ContextualImageTransitionBackDelegate {

let album = PhotoAlbum()

Expand Down Expand Up @@ -130,12 +130,16 @@ public class PhotoAlbumExampleViewController: UICollectionViewController, PhotoA
public override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let viewController = PhotoAlbumViewController(album: album)
viewController.currentPhoto = album.photos[indexPath.row]
viewController.transitionController.transition = PhotoAlbumTransition(delegate: self)
viewController.transitionController.transitions = [
ContextualImageTransition(backDelegate: self, foreDelegate: viewController),
SlideUpTransition(target: .target(viewController.toolbar))
]
present(viewController, animated: true)
}

fileprivate func contextView(forAlbumViewController: PhotoAlbumViewController) -> UIImageView? {
let currentPhoto = forAlbumViewController.currentPhoto
func backContextView(for transition: ContextualImageTransition,
with foreViewController: UIViewController) -> UIImageView? {
let currentPhoto = (foreViewController as! PhotoAlbumViewController).currentPhoto
guard let photoIndex = album.identifierToIndex[currentPhoto.uuid] else {
return nil
}
Expand All @@ -151,9 +155,10 @@ public class PhotoAlbumExampleViewController: UICollectionViewController, PhotoA
}
}

private class PhotoAlbumViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
private class PhotoAlbumViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, ContextualImageTransitionForeDelegate {

var collectionView: UICollectionView!
let toolbar = UIToolbar()
var currentPhoto: Photo

let album: PhotoAlbum
Expand Down Expand Up @@ -197,6 +202,11 @@ private class PhotoAlbumViewController: UIViewController, UICollectionViewDataSo
collectionView.bounds = extendedBounds

view.addSubview(collectionView)

let toolbarSize = toolbar.sizeThatFits(view.bounds.size)
toolbar.frame = .init(x: 0, y: view.bounds.height - toolbarSize.height,
width: toolbarSize.width, height: toolbarSize.height)
view.addSubview(toolbar)
}

override func viewDidLayoutSubviews() {
Expand All @@ -219,6 +229,14 @@ private class PhotoAlbumViewController: UIViewController, UICollectionViewDataSo
return .lightContent
}

// MARK: ContextualImageTransitionForeDelegate

func foreContextView(for transition: ContextualImageTransition) -> UIImageView? {
return (collectionView.cellForItem(at: indexPathForCurrentPhoto()) as! PhotoCollectionViewCell).imageView
}

// MARK: UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return album.photos.count
}
Expand All @@ -232,107 +250,19 @@ private class PhotoAlbumViewController: UIViewController, UICollectionViewDataSo
return cell
}

// MARK: UICollectionViewDelegate

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
dismiss(animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
currentPhoto = album.photos[indexPathForCurrentPhoto().item]
}

func indexPathForCurrentPhoto() -> IndexPath {
return collectionView.indexPathsForVisibleItems.first!
}
}

private protocol PhotoAlbumTransitionDelegate {
func contextView(forAlbumViewController: PhotoAlbumViewController) -> UIImageView?
}

private class PhotoAlbumTransition: NSObject, Transition, TransitionWithFallback {
// MARK: Private

// Store the context for the lifetime of the transition.
let delegate: PhotoAlbumTransitionDelegate
init(delegate: PhotoAlbumTransitionDelegate) {
self.delegate = delegate
}

func fallbackTransition(with context: TransitionContext) -> Transition? {
if delegate.contextView(forAlbumViewController: context.foreViewController as! PhotoAlbumViewController) != nil {
return self
}
return nil
}

func start(with context: TransitionContext) {
guard let contextView = delegate.contextView(forAlbumViewController: context.foreViewController as! PhotoAlbumViewController) else {
return
}

// A small helper function for creating bi-directional animations.
// See https://github.com/material-motion/motion-animator-objc for a more versatile
// bidirectional Core Animation implementation.
let addAnimationToLayer: (CABasicAnimation, CALayer) -> Void = { animation, layer in
if context.direction == .backward {
let swap = animation.fromValue
animation.fromValue = animation.toValue
animation.toValue = swap
}
layer.add(animation, forKey: animation.keyPath)
layer.setValue(animation.toValue, forKeyPath: animation.keyPath!)
}

let snapshotter = TransitionViewSnapshotter(containerView: context.containerView)
context.defer {
snapshotter.removeAllSnapshots()
}

let foreVC = context.foreViewController as! PhotoAlbumViewController
let foreImageView = (foreVC.collectionView.cellForItem(at: foreVC.indexPathForCurrentPhoto()) as! PhotoCollectionViewCell).imageView
let imageSize = foreImageView.image!.size

let fitScale = min(foreImageView.bounds.width / imageSize.width,
foreImageView.bounds.height / imageSize.height)
let fitSize = CGSize(width: fitScale * imageSize.width, height: fitScale * imageSize.height)

foreImageView.isHidden = true
context.defer {
foreImageView.isHidden = false
}

CATransaction.begin()
CATransaction.setCompletionBlock {
context.transitionDidEnd()
}

let fadeIn = CABasicAnimation(keyPath: "opacity")
fadeIn.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
fadeIn.fromValue = 0
fadeIn.toValue = 1
addAnimationToLayer(fadeIn, context.foreViewController.view.layer)

let snapshotContextView = snapshotter.snapshot(of: contextView,
isAppearing: context.direction == .backward)

let shift = CASpringAnimation(keyPath: "position")
shift.damping = 500
shift.stiffness = 1000
shift.mass = 3
shift.duration = 0.5
shift.fromValue = snapshotContextView.layer.position
shift.toValue = CGPoint(x: context.foreViewController.view.bounds.midX,
y: context.foreViewController.view.bounds.midY)
addAnimationToLayer(shift, snapshotContextView.layer)

let expansion = CASpringAnimation(keyPath: "bounds.size")
expansion.damping = 500
expansion.stiffness = 1000
expansion.mass = 3
expansion.duration = 0.5
expansion.fromValue = snapshotContextView.layer.bounds.size
expansion.toValue = fitSize
addAnimationToLayer(expansion, snapshotContextView.layer)

CATransaction.commit()
private func indexPathForCurrentPhoto() -> IndexPath {
return collectionView.indexPathsForVisibleItems.first!
}
}
36 changes: 36 additions & 0 deletions examples/TransitionTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2017-present The Material Motion Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import UIKit
import MotionTransitioning

// A potential target for a transition's motion.
enum TransitionTarget {
case backView
case foreView
case target(UIView)

func resolve(with context: TransitionContext) -> UIView {
switch self {
case .backView:
return context.backViewController.view
case .foreView:
return context.foreViewController.view
case .target(let view):
return view
}
}
}
24 changes: 22 additions & 2 deletions examples/apps/Catalog/TransitionsCatalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
668E288B1F4F68D2008A4550 /* ContextualExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668E288A1F4F68D2008A4550 /* ContextualExample.swift */; };
668E288E1F5066AA008A4550 /* PhotoAlbumExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668E288D1F5066AA008A4550 /* PhotoAlbumExample.swift */; };
668E28901F50673A008A4550 /* PhotoAlbum.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 668E288F1F50673A008A4550 /* PhotoAlbum.xcassets */; };
668E28971F571CA4008A4550 /* SlideUpTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668E28961F571CA4008A4550 /* SlideUpTransition.swift */; };
668E28991F5729C1008A4550 /* TransitionTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668E28981F5729C1008A4550 /* TransitionTarget.swift */; };
668E289B1F572A9D008A4550 /* ContextualImageTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 668E289A1F572A9D008A4550 /* ContextualImageTransition.swift */; };
66A320FC1F1E716600E2EAC3 /* NavControllerFadeExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664CC3D91F1E6F3000B80804 /* NavControllerFadeExample.swift */; };
66BBC75E1ED37DAD0015CB9B /* FadeExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66BBC75D1ED37DAD0015CB9B /* FadeExample.swift */; };
66BBC76D1ED4C8790015CB9B /* ExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66BBC7691ED4C8790015CB9B /* ExampleViewController.swift */; };
Expand Down Expand Up @@ -75,10 +78,13 @@
667A3F4B1DEE269400CB3A99 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
667A3F4D1DEE269400CB3A99 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
667A3F531DEE273000CB3A99 /* TableOfContents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableOfContents.swift; sourceTree = "<group>"; };
668E28841F4F5389008A4550 /* FadeTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FadeTransition.swift; sourceTree = "<group>"; };
668E28841F4F5389008A4550 /* FadeTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FadeTransition.swift; path = transitions/FadeTransition.swift; sourceTree = "<group>"; };
668E288A1F4F68D2008A4550 /* ContextualExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContextualExample.swift; sourceTree = "<group>"; };
668E288D1F5066AA008A4550 /* PhotoAlbumExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoAlbumExample.swift; sourceTree = "<group>"; };
668E288F1F50673A008A4550 /* PhotoAlbum.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = PhotoAlbum.xcassets; sourceTree = "<group>"; };
668E28961F571CA4008A4550 /* SlideUpTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SlideUpTransition.swift; path = transitions/SlideUpTransition.swift; sourceTree = "<group>"; };
668E28981F5729C1008A4550 /* TransitionTarget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionTarget.swift; sourceTree = "<group>"; };
668E289A1F572A9D008A4550 /* ContextualImageTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContextualImageTransition.swift; path = transitions/ContextualImageTransition.swift; sourceTree = "<group>"; };
66BBC75D1ED37DAD0015CB9B /* FadeExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FadeExample.swift; path = ../FadeExample.swift; sourceTree = "<group>"; };
66BBC7691ED4C8790015CB9B /* ExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleViewController.swift; sourceTree = "<group>"; };
66BBC76A1ED4C8790015CB9B /* ExampleViews.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleViews.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -181,6 +187,7 @@
668E28861F4F66C7008A4550 /* Custom presentation */,
668E28831F4F5371008A4550 /* Fade transition */,
668E288C1F506698008A4550 /* Photo album */,
668E28951F571C8F008A4550 /* Transitions */,
072A063A1EEE26A900B9B5FC /* MenuExample.swift */,
);
name = examples;
Expand Down Expand Up @@ -233,7 +240,6 @@
66BBC7731ED729A70015CB9B /* FadeExample.h */,
66BBC7741ED729A70015CB9B /* FadeExample.m */,
664CC3D91F1E6F3000B80804 /* NavControllerFadeExample.swift */,
668E28841F4F5389008A4550 /* FadeTransition.swift */,
);
name = "Fade transition";
path = transitions;
Expand Down Expand Up @@ -263,6 +269,17 @@
name = "Photo album";
sourceTree = "<group>";
};
668E28951F571C8F008A4550 /* Transitions */ = {
isa = PBXGroup;
children = (
668E289A1F572A9D008A4550 /* ContextualImageTransition.swift */,
668E28841F4F5389008A4550 /* FadeTransition.swift */,
668E28961F571CA4008A4550 /* SlideUpTransition.swift */,
668E28981F5729C1008A4550 /* TransitionTarget.swift */,
);
name = Transitions;
sourceTree = "<group>";
};
66BBC7681ED4C8790015CB9B /* supplemental */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -522,14 +539,17 @@
buildActionMask = 2147483647;
files = (
666FAA841D384A6B000363DA /* AppDelegate.swift in Sources */,
668E28991F5729C1008A4550 /* TransitionTarget.swift in Sources */,
66BBC76F1ED4C8790015CB9B /* HexColor.swift in Sources */,
668E28971F571CA4008A4550 /* SlideUpTransition.swift in Sources */,
66BBC7751ED729A80015CB9B /* FadeExample.m in Sources */,
072A063B1EEE26A900B9B5FC /* MenuExample.swift in Sources */,
66BBC76D1ED4C8790015CB9B /* ExampleViewController.swift in Sources */,
667A3F541DEE273000CB3A99 /* TableOfContents.swift in Sources */,
66A320FC1F1E716600E2EAC3 /* NavControllerFadeExample.swift in Sources */,
668E288E1F5066AA008A4550 /* PhotoAlbumExample.swift in Sources */,
66BBC7701ED4C8790015CB9B /* Layout.swift in Sources */,
668E289B1F572A9D008A4550 /* ContextualImageTransition.swift in Sources */,
6629151E1ED5E0E0002B9A5D /* CustomPresentationExample.swift in Sources */,
668E288B1F4F68D2008A4550 /* ContextualExample.swift in Sources */,
66BBC76E1ED4C8790015CB9B /* ExampleViews.swift in Sources */,
Expand Down
Loading

0 comments on commit 8653958

Please sign in to comment.