22 May 2017

DeckTransition – A library to recreate the iOS 10 Apple Music now playing transition

CI Status Version License Platform

DeckTransition is an attempt to recreate the card-like transition found in the iOS 10 Apple Music and iMessage apps.

Hereʼs a GIF showing it in action.

Demo

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 3
  • iOS 9 or later

Installation

DeckTransition is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "DeckTransition"

Usage

Basics

Set modalPresentationCapturesStatusBarAppearance to true in your modal viewcontroller, and override the preferredStatusBarStyle variable to return .lightContent.

The background colour for the presentation can be changed by changing the backgroundColor property of the window. This is .black by default.

Presentation

The transition can be called from code or using a storyboard.

To use via storyboards, just setup a custom segue (kind set to custom), and set the class to DeckSegue.

Hereʼs a snippet showing usage via code. Just replace ModalViewController() with your viewcontroller’s class and youʼre good to go.

let modal = ModalViewController()
let transitionDelegate = DeckTransitioningDelegate()
modal.transitioningDelegate = transitionDelegate
modal.modalPresentationStyle = .custom
present(modal, animated: true, completion: nil)

Dismissal

This is the part where it gets a bit tricky. If youʼve got a fixed-sized i.e. non-scrolling modal, feel free to just skip the rest of this section. Swipe-to-dismiss will work perfectly for you

For modals which have a vertically scrolling layout, the dismissal gesture should be fired only when the view is scrolled to the top. To achieve this behaviour, you need to modify the isDismissEnabled property of the DeckTransitioningDelegate. (You can also set isDismissEnabled to false if you want to disable the swipe-to-dismiss UI.)

The one issue with doing this in response to the scrollviewʼs contentOffset is momentum scrolling. When the user pans from top the bottom, once the top of the scrollview is reached (contentOffset.y is 0), the dismiss gesture should take over and the scrollview should stop scrolling, not showing the usual iOS bounce effect. The dismiss gesture, however, only responds to pans and not swipes, so should you swipe and not pan, the scrollview will scroll to the top and abruptly stop (as the contentOffset.y is 0) without the usual iOS bounce effect.

I’ve found a temporary workaround for this, the code for this can be found below. Itʼs a bit messy right now, but is the only workaround Iʼve found for this issue (so far). It has one caveat, in that it fails utterly miserably when using with a scrollview whose backgroundColor isnʼt .clear. Iʼll update this project if/when I find a better solution.

Dismissal code for scrolling modals

First up, make your modal viewcontroller conform to UIScrollViewDelegate (or UITableViewDelegate/UITextFieldDelegate, as the case may be), and assign self as the scrollview’s delegate.

Next, add this method to your modal viewcontroller, swapping in your scrollviewʼs variable for textView.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard scrollView.isEqual(textView) else {
        return
    }

    if let delegate = transitioningDelegate as? DeckTransitioningDelegate {
        if scrollView.contentOffset.y > 0 {
            // Normal behaviour if the `scrollView` isn't scrolled to the top
            scrollView.bounces = true
            delegate.isDismissEnabled = false
        } else {
            if scrollView.isDecelerating {
                // If the `scrollView` is scrolled to the top but is decelerating
                // that means a swipe has been performed. The view and scrollview are
                // both translated in response to this.
                view.transform = CGAffineTransform(translationX: 0, y: -scrollView.contentOffset.y)
                scrollView.transform = CGAffineTransform(translationX: 0, y: scrollView.contentOffset.y)
            } else {
                // If the user has panned to the top, the scrollview doesnʼt bounce and
                // the dismiss gesture is enabled.
                scrollView.bounces = false
                delegate.isDismissEnabled = true
            }
        }
    }
}

Author

Written by Harshil Shah. You can find me on Twitter if you have any suggestions, requests, or just want to talk!

If you use this library in your application, please let me know about it.

License

DeckTransition is available under the MIT license. See the LICENSE file for more info.

 

 

Source: https://github.com/HarshilShah/DeckTransition

Leave a Reply

%d bloggers like this: