Skip to content

Commit 3fb4757

Browse files
author
Pietro Rea
committed
Merging 1.0.0 branch
1 parent 4f77532 commit 3fb4757

6 files changed

Lines changed: 119 additions & 49 deletions

File tree

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="r9E-DI-q2Y">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="V4X-9O-Iwd">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
@@ -10,17 +10,18 @@
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
1212
<scenes>
13-
<!--View Controller-->
13+
<!--Presenting-->
1414
<scene sceneID="3Il-X6-dIP">
1515
<objects>
16-
<viewController id="r9E-DI-q2Y" customClass="ViewController" customModule="SpringAnimationController" customModuleProvider="target" sceneMemberID="viewController">
16+
<viewController title="Presenting" id="r9E-DI-q2Y" customClass="ViewController" customModule="SpringAnimationController_Example" customModuleProvider="target" sceneMemberID="viewController">
1717
<view key="view" contentMode="scaleToFill" id="08J-eO-oD5">
1818
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1919
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2020
<subviews>
2121
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nyn-IW-b3z">
22-
<rect key="frame" x="161" y="578" width="53" height="30"/>
23-
<state key="normal" title="Present"/>
22+
<rect key="frame" x="126" y="575" width="123" height="33"/>
23+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
24+
<state key="normal" title="Present Modally"/>
2425
<connections>
2526
<action selector="presentViewController:" destination="r9E-DI-q2Y" eventType="touchUpInside" id="w1A-hu-MM1"/>
2627
</connections>
@@ -33,50 +34,75 @@
3334
</constraints>
3435
<viewLayoutGuide key="safeArea" id="RYU-TX-ztu"/>
3536
</view>
37+
<navigationItem key="navigationItem" title="Presenting" id="Ykk-UK-gX0"/>
3638
<connections>
3739
<outlet property="presentButton" destination="nyn-IW-b3z" id="bZ7-7L-C00"/>
3840
</connections>
3941
</viewController>
4042
<placeholder placeholderIdentifier="IBFirstResponder" id="Ckr-64-9eL" sceneMemberID="firstResponder"/>
4143
</objects>
42-
<point key="canvasLocation" x="58" y="129"/>
44+
<point key="canvasLocation" x="996" y="128.18590704647679"/>
4345
</scene>
44-
<!--Presented View Controller-->
46+
<!--Presented-->
4547
<scene sceneID="YQk-Os-YLa">
4648
<objects>
47-
<viewController storyboardIdentifier="PresentedViewController" id="5aQ-jH-G1A" customClass="PresentedViewController" customModule="SpringAnimationController" customModuleProvider="target" sceneMemberID="viewController">
49+
<viewController storyboardIdentifier="PresentedViewController" title="Presented" id="5aQ-jH-G1A" customClass="PresentedViewController" customModule="SpringAnimationController_Example" customModuleProvider="target" sceneMemberID="viewController">
4850
<view key="view" contentMode="scaleToFill" id="rn3-3u-nMc">
4951
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
5052
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
51-
<subviews>
52-
<navigationBar contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="VBS-dl-lL6">
53-
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
54-
<items>
55-
<navigationItem title="Title" id="qaP-qK-Vz9">
56-
<barButtonItem key="leftBarButtonItem" title="Close" id="Cc7-P1-bWf">
57-
<connections>
58-
<action selector="close:" destination="5aQ-jH-G1A" id="fns-HL-6rA"/>
59-
</connections>
60-
</barButtonItem>
61-
</navigationItem>
62-
</items>
63-
</navigationBar>
64-
</subviews>
6553
<color key="backgroundColor" red="0.60392156860000001" green="0.27450980390000002" blue="0.81960784310000001" alpha="1" colorSpace="calibratedRGB"/>
66-
<constraints>
67-
<constraint firstItem="VBS-dl-lL6" firstAttribute="trailing" secondItem="KBe-yB-s40" secondAttribute="trailing" id="Dp6-qS-AgP"/>
68-
<constraint firstItem="VBS-dl-lL6" firstAttribute="leading" secondItem="KBe-yB-s40" secondAttribute="leading" id="JCx-bB-MC8"/>
69-
<constraint firstItem="VBS-dl-lL6" firstAttribute="top" secondItem="rn3-3u-nMc" secondAttribute="top" id="pUn-o0-61z"/>
70-
</constraints>
7154
<viewLayoutGuide key="safeArea" id="KBe-yB-s40"/>
7255
</view>
56+
<navigationItem key="navigationItem" title="Presented" id="7uG-Lq-xnj">
57+
<barButtonItem key="leftBarButtonItem" title="Done" id="rjw-hG-t8s">
58+
<connections>
59+
<action selector="close:" destination="5aQ-jH-G1A" id="fX8-q1-GHz"/>
60+
</connections>
61+
</barButtonItem>
62+
</navigationItem>
7363
<connections>
74-
<outlet property="closeBarButtonItem" destination="Cc7-P1-bWf" id="JET-dI-Jd4"/>
64+
<outlet property="closeBarButtonItem" destination="rjw-hG-t8s" id="LU8-vq-uXl"/>
7565
</connections>
7666
</viewController>
7767
<placeholder placeholderIdentifier="IBFirstResponder" id="Z1L-TD-S68" userLabel="First Responder" sceneMemberID="firstResponder"/>
7868
</objects>
79-
<point key="canvasLocation" x="780" y="128"/>
69+
<point key="canvasLocation" x="2452" y="126.38680659670166"/>
70+
</scene>
71+
<!--Navigation Controller-->
72+
<scene sceneID="XzB-ub-cUf">
73+
<objects>
74+
<navigationController storyboardIdentifier="PresentedViewControllerNavigationController" automaticallyAdjustsScrollViewInsets="NO" id="7Zc-nO-qTG" sceneMemberID="viewController">
75+
<toolbarItems/>
76+
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="rnF-12-xth">
77+
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
78+
<autoresizingMask key="autoresizingMask"/>
79+
</navigationBar>
80+
<nil name="viewControllers"/>
81+
<connections>
82+
<segue destination="5aQ-jH-G1A" kind="relationship" relationship="rootViewController" id="jOw-yU-Qi8"/>
83+
</connections>
84+
</navigationController>
85+
<placeholder placeholderIdentifier="IBFirstResponder" id="X2g-jS-hcY" userLabel="First Responder" sceneMemberID="firstResponder"/>
86+
</objects>
87+
<point key="canvasLocation" x="1719.2" y="127.28635682158921"/>
88+
</scene>
89+
<!--Navigation Controller-->
90+
<scene sceneID="40C-vr-8jv">
91+
<objects>
92+
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="V4X-9O-Iwd" sceneMemberID="viewController">
93+
<toolbarItems/>
94+
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="Wqn-5O-w7S">
95+
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
96+
<autoresizingMask key="autoresizingMask"/>
97+
</navigationBar>
98+
<nil name="viewControllers"/>
99+
<connections>
100+
<segue destination="r9E-DI-q2Y" kind="relationship" relationship="rootViewController" id="2p2-CK-xWO"/>
101+
</connections>
102+
</navigationController>
103+
<placeholder placeholderIdentifier="IBFirstResponder" id="3dJ-c6-NNB" userLabel="First Responder" sceneMemberID="firstResponder"/>
104+
</objects>
105+
<point key="canvasLocation" x="56.799999999999997" y="128.18590704647679"/>
80106
</scene>
81107
</scenes>
82108
</document>

Example/SpringAnimationController/PresentedViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class PresentedViewController: UIViewController {
1515
override func viewDidLoad() {
1616
super.viewDidLoad()
1717

18-
// Do any additional setup after loading the view.
1918
}
2019

2120
@IBAction func close(_ sender: UIBarButtonItem) {

Example/SpringAnimationController/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ViewController: UIViewController {
2121
@IBAction func presentViewController(_ sender: UIButton) {
2222

2323
let storyboard = UIStoryboard(name: "Main", bundle: nil)
24-
guard let presentedViewController = storyboard.instantiateViewController(withIdentifier: "PresentedViewController") as? PresentedViewController else {
24+
guard let presentedViewController = storyboard.instantiateViewController(withIdentifier: "PresentedViewControllerNavigationController") as? UINavigationController else {
2525
return
2626
}
2727

README.md

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,60 @@
11
# SpringAnimationController
22

3-
[![CI Status](https://img.shields.io/travis/pietrorea/SpringAnimationController.svg?style=flat)](https://travis-ci.org/pietrorea/SpringAnimationController)
43
[![Version](https://img.shields.io/cocoapods/v/SpringAnimationController.svg?style=flat)](https://cocoapods.org/pods/SpringAnimationController)
54
[![License](https://img.shields.io/cocoapods/l/SpringAnimationController.svg?style=flat)](https://cocoapods.org/pods/SpringAnimationController)
65
[![Platform](https://img.shields.io/cocoapods/p/SpringAnimationController.svg?style=flat)](https://cocoapods.org/pods/SpringAnimationController)
76

7+
`SpringAnimationController` provides a subtle way to spice up the default modal presentation style on iOS using spring animations. Simply implement the `UIViewControllerTransitioningDelegate` protocol and return this animation controller to add some bounce at the top of your modal presentation.
8+
9+
![Demo](https://user-images.githubusercontent.com/1264371/41214965-f2636130-6d1b-11e8-8ac3-5e415d56664c.gif)
10+
11+
## Usage
12+
13+
First, set up the view controller that you want to present modally and trigger the model presentation as you normally would.
14+
15+
```swift
16+
let viewControllerToPresent = PresentedViewController()
17+
viewControllerToPresent.transitioningDelegate = self
18+
viewControllerToPresent.modalPresentationStyle = .custom
19+
20+
present(viewControllerToPresent, animated: true)
21+
```
22+
23+
Next, implement `UIViewControllerTransitioningDelegate` to return an instance of `SpringyAnimationController`
24+
25+
```swift
26+
func animationController(forPresented presented: UIViewController,
27+
presenting: UIViewController,
28+
source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
29+
30+
let bouncyTransition = SpringAnimationController(isPresenting: true)
31+
return bouncyTransition
32+
}
33+
34+
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
35+
36+
let bouncyTransition = SpringAnimationController(isPresenting: false)
37+
return bouncyTransition
38+
}
39+
```
40+
You can also customize the animation duration, spring damping and spring velocity before returning the animation controller.
41+
42+
```swift
43+
let bouncyTransition = SpringAnimationController(isPresenting: true)
44+
bouncyTransition.transitionDuration = 0.35
45+
bouncyTransition.springDamping = 0.5
46+
bouncyTransition.springVelocity = 0.7
47+
```
48+
849
## Example
950

10-
To run the example project, clone the repo, and run `pod install` from the Example directory first.
51+
To run the example project, simply run `pod try SpringAnimationController` .
1152

1253
## Requirements
1354

14-
## Installation
55+
iOS 10 and above
56+
57+
## Installation Instructions
1558

1659
SpringAnimationController is available through [CocoaPods](https://cocoapods.org). To install
1760
it, simply add the following line to your Podfile:
@@ -20,10 +63,6 @@ it, simply add the following line to your Podfile:
2063
pod 'SpringAnimationController'
2164
```
2265

23-
## Author
24-
25-
pietrorea, pietro@sweetpeamobile.com
26-
2766
## License
2867

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

SpringAnimationController.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
s.license = { :type => 'MIT', :file => 'LICENSE' }
1919
s.author = { 'pietrorea' => 'pietro@sweetpeamobile.com' }
2020
s.social_media_url = 'https://twitter.com/pietrorea'
21-
s.source = { :git => 'https://github.com/pietrorea/SpringAnimationController.git', :branch => s.version.to_s }
21+
s.source = { :git => 'https://github.com/pietrorea/SpringAnimationController.git', :tag => s.version.to_s }
2222
s.ios.deployment_target = '10.0'
2323
s.swift_version = '4.1'
2424

SpringAnimationController/Classes/SpringAnimationController.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
//
2-
// BouncyTransition.swift
3-
// BouncyTransition
2+
// SpringAnimationController.swift
3+
// SpringAnimationController
44
//
55
// Created by Pietro Rea on 6/8/18.
66
// Copyright © 2018 Pietro Rea. All rights reserved.
77
//
88

99
import UIKit
1010

11+
/// The `SpringAnimationController` class. It conforms to `UIViewControllerAnimatedTransitioning` and plugs into the custom view controller transition infrastructure provided by iOS.
1112
public class SpringAnimationController: NSObject {
1213

1314
private static let defaultTransitionDuration: TimeInterval = 0.45
14-
private static let defaultDelay: TimeInterval = 0.0
1515
private static let defaultSpringDamping: CGFloat = 0.70
1616
private static let defaultInitialSpringVelocity: CGFloat = 0.9
1717

18-
private let isPresenting: Bool
18+
/// Determines how long the animation will take. Defaults to 0.45 seconds.
19+
public var transitionDuration: TimeInterval = SpringAnimationController.defaultTransitionDuration
1920

20-
var transitionDuration: TimeInterval = SpringAnimationController.defaultTransitionDuration
21-
var delay: TimeInterval = SpringAnimationController.defaultDelay
22-
var springDamping: CGFloat = SpringAnimationController.defaultSpringDamping
23-
var springVelocity: CGFloat = SpringAnimationController.defaultInitialSpringVelocity
21+
/// Determines how smoothly the spring animation will decelerate to its final position. If `springDamping` is 1, there will be no bouncing. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. The default value is 0.7. Only supported if `isPresenting` is true.
22+
public var springDamping: CGFloat = SpringAnimationController.defaultSpringDamping
2423

24+
/// Determines how fast the view controller was traveling upwards before attaching to the invisible spring at the top of the screen. Higher velocities will result in more oscillation. The default value is 0.9. Only supported if `isPresenting` is true.
25+
public var springVelocity: CGFloat = SpringAnimationController.defaultInitialSpringVelocity
26+
27+
let isPresenting: Bool
28+
29+
/// Designated initializer for the class.
30+
///
31+
/// - Parameter isPresenting: True if the view controller is being presented (upwards animation). False if the view controller is being dismissed (downwards animation). Spring-like bouncing is only supported if `isPresenting` is true.
2532
public init(isPresenting: Bool) {
2633
self.isPresenting = isPresenting
2734
}
2835
}
2936

30-
3137
extension SpringAnimationController: UIViewControllerAnimatedTransitioning {
3238

3339
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
@@ -49,7 +55,7 @@ extension SpringAnimationController: UIViewControllerAnimatedTransitioning {
4955
newFrame.origin.y = presentedVC.view.frame.size.height
5056
presentedVC.view.frame = newFrame
5157

52-
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: delay, usingSpringWithDamping: springDamping, initialSpringVelocity: springVelocity, options: [], animations: {
58+
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: springDamping, initialSpringVelocity: springVelocity, options: [], animations: {
5359

5460
let finalFrame = transitionContext.finalFrame(for: presentedVC)
5561
presentedVC.view.frame = finalFrame

0 commit comments

Comments
 (0)