Skip to content

Commit

Permalink
Added Stork Controller
Browse files Browse the repository at this point in the history
- added Stork Controller (as Apple Music or Mail app)
- also rename SPStatusBarStyle to SPStatusBar
- add paramter statusBar for func wrapToNavigationController (set prefered status bar style for navigation controller)
  • Loading branch information
ivanvorobei committed Nov 28, 2018
1 parent 2b47d02 commit 5d67c4c
Show file tree
Hide file tree
Showing 13 changed files with 1,031 additions and 644 deletions.
1,284 changes: 654 additions & 630 deletions example/request-permission.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Binary file not shown.
14 changes: 10 additions & 4 deletions source/sparrow/extension/SPUIViewControllerExtenshion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ import Photos

extension UIViewController {

func wrapToNavigationController() -> UINavigationController {
return UINavigationController(rootViewController: self)
}

func present(_ viewControllerToPresent: UIViewController, completion: (() -> Swift.Void)? = nil) {
self.present(viewControllerToPresent, animated: true, completion: completion)
}

@objc func dismiss() {
self.dismiss(animated: true, completion: nil)
}

func wrapToNavigationController(statusBar: SPStatusBar = .dark) -> UINavigationController {
let controller = SPStatusBarManagerNavigationController(rootViewController: self)
controller.statusBar = statusBar
return controller
}
}

//MARK: - Keyboard
Expand Down Expand Up @@ -150,6 +152,10 @@ extension UIViewController {
var navigationBarHeight: CGFloat {
return self.navigationController?.navigationBar.frame.height ?? 0
}

var statusBarHeight: CGFloat {
return UIApplication.shared.statusBarFrame.height
}
}

extension UIViewController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class SPPermissionDialogController: SPBaseViewController {
self.areaView.transform = .identity
self.modalPresentationStyle = .overCurrentContext
self.modalPresentationCapturesStatusBarAppearance = true
self.statusBarType = (viewController.preferredStatusBarStyle == .default) ? .dark : .light
self.statusBar = (viewController.preferredStatusBarStyle == .default) ? .dark : .light
viewController.present(self, animated: false, completion: {
self.isHiddenStatusBar = true
self.areaView.center = CGPoint.init(
Expand Down
2 changes: 1 addition & 1 deletion source/sparrow/types/SPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import UIKit

public enum SPStatusBarStyle {
public enum SPStatusBar {
case dark
case light
}
Expand Down
2 changes: 1 addition & 1 deletion source/sparrow/ui/controllers/SPHiderViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SPHiderViewController: SPBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.statusBarType = .light
self.statusBar = .light

self.view.backgroundColor = UIColor.clear

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SPNativeTableViewController: SPBaseTableViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.statusBarType = .dark
self.statusBar = .dark

self.tableView = UITableView.init(frame: self.view.bounds, style: UITableView.Style.grouped)
self.setPrefersLargeNavigationTitle("Title")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import UIKit

public class SPStatusBarManagerViewController: UIViewController {

var statusBarType: SPStatusBarStyle = .dark {
var statusBar: SPStatusBar = .dark {
didSet {
UIView.animate(withDuration: 0.3) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
Expand All @@ -32,7 +32,7 @@ public class SPStatusBarManagerViewController: UIViewController {
}

override public var preferredStatusBarStyle: UIStatusBarStyle {
switch self.statusBarType {
switch self.statusBar {
case .dark:
return .default
case .light:
Expand All @@ -59,7 +59,7 @@ public class SPStatusBarManagerViewController: UIViewController {

public class SPStatusBarManagerTableViewController: UITableViewController {

var statusBarType: SPStatusBarStyle = .dark {
var statusBar: SPStatusBar = .dark {
didSet {
UIView.animate(withDuration: 0.3) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
Expand All @@ -68,7 +68,7 @@ public class SPStatusBarManagerTableViewController: UITableViewController {
}

override public var preferredStatusBarStyle: UIStatusBarStyle {
switch self.statusBarType {
switch self.statusBar {
case .dark:
return .default
case .light:
Expand All @@ -92,3 +92,46 @@ public class SPStatusBarManagerTableViewController: UITableViewController {
return isHiddenStatusBar
}
}

public class SPStatusBarManagerNavigationController: UINavigationController {

var statusBar: SPStatusBar = .dark {
didSet {
UIView.animate(withDuration: 0.3) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}

override public var preferredStatusBarStyle: UIStatusBarStyle {
switch self.statusBar {
case .dark:
return .default
case .light:
return .lightContent
}
}

override public var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}

public var isHiddenStatusBar: Bool = false {
didSet {
UIView.animate(withDuration: 0.3) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}

override public var prefersStatusBarHidden: Bool {
return isHiddenStatusBar
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIView.animate(withDuration: 0.3) { () -> Void in
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
4 changes: 2 additions & 2 deletions source/sparrow/ui/controllers/SPWelcomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SPWelcomeViewController: SPBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.statusBarType = self.data.statusBarStyle
self.statusBar = self.data.statusBarStyle
self.view.backgroundColor = self.data.backgroundColor

self.imageView.setImage(image: self.data.image, animatable: false)
Expand Down Expand Up @@ -213,6 +213,6 @@ struct SPWelcomeData {
var color: UIColor = SPNativeStyleKit.Colors.blue
var backgroundColor: UIColor = UIColor.white
var textColor: UIColor = UIColor.black
var statusBarStyle: SPStatusBarStyle = .dark
var statusBarStyle: SPStatusBar = .dark
var complection: (_ button: SPNativeOS11Button) -> () = { _ in }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// The MIT License (MIT)
// Copyright © 2017 Ivan Vorobei (hello@ivanvorobei.by)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit

final class SPStorkDismissingAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

guard let presentedViewController = transitionContext.viewController(forKey: .from) else {
return
}

let containerView = transitionContext.containerView
let offscreenFrame = CGRect(x: 0, y: containerView.bounds.height, width: containerView.bounds.width, height: containerView.bounds.height)

UIView.animate(
withDuration: transitionDuration(using: transitionContext),
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseIn,
animations: {
presentedViewController.view.frame = offscreenFrame
}) { finished in
transitionContext.completeTransition(finished)
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// The MIT License (MIT)
// Copyright © 2017 Ivan Vorobei (hello@ivanvorobei.by)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit

final class SPStorkPresentationController: UIPresentationController, UIGestureRecognizerDelegate {

var isSwipeToDismissEnabled: Bool = true
var transitioningDelegate: SPStorkTransitioningDelegate?
private var pan: UIPanGestureRecognizer?

private var scaleFactor: CGFloat {
return 349 / 375
}

private var topSpace: CGFloat {
if presentingViewController.statusBarHeight < 25 {
return 30
} else {
return presentingViewController.statusBarHeight
}
}

private var transform: CGAffineTransform {
let translate: CGFloat = ((self.containerView?.frame.height ?? 0) * (1 - self.scaleFactor)) / 2
return CGAffineTransform.identity.scaledBy(x: scaleFactor, y: scaleFactor).translatedBy(x: 0, y: -translate + topSpace)
}

private var alpha: CGFloat {
return 0.51
}

override var frameOfPresentedViewInContainerView: CGRect {
if let containerView = self.containerView {
let yOffset = topSpace + 13
return CGRect(x: 0, y: yOffset, width: containerView.bounds.width, height: containerView.bounds.height - yOffset)
} else {
return .zero
}
}

override func presentationTransitionWillBegin() {
guard let containerView = self.containerView, let presentedView = self.presentedView, let presentingView = self.presentingViewController.view else { return }

presentedView.addCornerRadiusAnimation(to: 10, duration: 0.6)
presentedView.layer.masksToBounds = true
presentingView.layer.cornerRadius = 10
presentingView.layer.masksToBounds = true
containerView.addSubview(presentedView)

guard let transitionCoordinator = self.presentingViewController.transitionCoordinator else { return }
transitionCoordinator.animateAlongsideTransition(in: self.presentingViewController.view, animation: { _ in
self.presentingViewController.view.transform = self.transform
self.presentingViewController.view.alpha = self.alpha
})
}

override func presentationTransitionDidEnd(_ completed: Bool) {
if self.isSwipeToDismissEnabled {
self.pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.pan!.delegate = self
self.pan!.maximumNumberOfTouches = 1
self.pan!.cancelsTouchesInView = false
self.presentedViewController.view.addGestureRecognizer(pan!)
}
}

override func dismissalTransitionWillBegin() {
guard let transitionCoordinator = presentingViewController.transitionCoordinator else { return }

transitionCoordinator.animate(alongsideTransition: { _ in
self.presentingViewController.view.alpha = 1
})

transitionCoordinator.animateAlongsideTransition(in: presentingViewController.view, animation: { _ in
self.presentingViewController.view.transform = CGAffineTransform.identity
})

self.presentingViewController.view.addCornerRadiusAnimation(to: 0, duration: 0.6)
}

override func dismissalTransitionDidEnd(_ completed: Bool) {
if let presentingView = self.presentingViewController.view {
presentingView.layer.cornerRadius = 0
presentingView.layer.masksToBounds = false
}
}
}

extension SPStorkPresentationController {

@objc fileprivate func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
guard gestureRecognizer.isEqual(pan), self.isSwipeToDismissEnabled else {
return
}

switch gestureRecognizer.state {
case .began:
gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: containerView)
case .changed:
if self.isSwipeToDismissEnabled {
let translation = gestureRecognizer.translation(in: presentedView)
updatePresentedViewForTranslation(inVerticalDirection: translation.y)
} else {
gestureRecognizer.setTranslation(.zero, in: presentedView)
}
case .ended:
let translation = gestureRecognizer.translation(in: presentedView).y
if translation >= 240 {
presentedViewController.dismiss(animated: true, completion: nil)
} else {
UIView.animate(
withDuration: 0.6,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
self.presentedView?.transform = .identity
self.presentingViewController.view.transform = self.transform
self.presentingViewController.view.alpha = self.alpha
})
}
default:
break
}
}

private func updatePresentedViewForTranslation(inVerticalDirection translation: CGFloat) {

let elasticThreshold: CGFloat = 120
let translationFactor: CGFloat = 1 / 2

if translation >= 0 {
let translationForModal: CGFloat = {
if translation >= elasticThreshold {
let frictionLength = translation - elasticThreshold
let frictionTranslation = 30 * atan(frictionLength / 120) + frictionLength / 10
return frictionTranslation + (elasticThreshold * translationFactor)
} else {
return translation * translationFactor
}
}()

self.presentedView?.transform = CGAffineTransform(translationX: 0, y: translationForModal)

let factor = 1 + (translationForModal / 6000)
self.presentingViewController.view.transform = self.transform.scaledBy(x: factor, y: factor)
self.presentingViewController.view.alpha = self.alpha + ((factor - 1) * 15)
}
}
}

0 comments on commit 5d67c4c

Please sign in to comment.