Skip to content

Commit

Permalink
Merge pull request #62 from maslovsa/fix/animation
Browse files Browse the repository at this point in the history
Add animation for add and remove operations
  • Loading branch information
MarioIannotta committed Jun 12, 2019
2 parents dfb0534 + cd29504 commit 1b29429
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 35 deletions.
38 changes: 34 additions & 4 deletions Example/PullUpController/UI/Main.storyboard
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="doG-7b-3D6">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="doG-7b-3D6">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -22,10 +22,10 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
</mapView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="iMQ-X7-eJt">
<rect key="frame" x="20" y="84" width="250" height="137"/>
<rect key="frame" x="20" y="84" width="250" height="264"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" distribution="fillEqually" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="dfq-ok-AvZ">
<rect key="frame" x="15" y="10" width="220" height="117"/>
<rect key="frame" x="15" y="10" width="220" height="244"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="IPI-H1-hVv">
<rect key="frame" x="0.0" y="0.0" width="220" height="53.5"/>
Expand Down Expand Up @@ -62,6 +62,34 @@
</segmentedControl>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="bC5-pY-cOk">
<rect key="frame" x="0.0" y="127" width="220" height="53.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Animate Show" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="K9M-Xa-0zK">
<rect key="frame" x="0.0" y="16.5" width="171" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="geH-Oa-eM4">
<rect key="frame" x="171" y="11.5" width="51" height="31"/>
</switch>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dZa-i4-BY8">
<rect key="frame" x="0.0" y="190.5" width="220" height="53.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Animate Hide" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="edd-Am-66x">
<rect key="frame" x="0.0" y="16.5" width="171" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="BZg-jf-DZo">
<rect key="frame" x="171" y="11.5" width="51" height="31"/>
</switch>
</subviews>
</stackView>
</subviews>
</stackView>
</subviews>
Expand Down Expand Up @@ -114,6 +142,8 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="animateHideSwitch" destination="BZg-jf-DZo" id="RnF-ZE-2lQ"/>
<outlet property="animateShowSwitch" destination="geH-Oa-eM4" id="8J4-pR-Uki"/>
<outlet property="initialStateSegmentedControl" destination="iWm-ZS-z7f" id="hG6-Cf-Ter"/>
<outlet property="mapView" destination="drN-fA-22T" id="EE5-qd-AEu"/>
<outlet property="sizeSliderView" destination="iMQ-X7-eJt" id="5B3-VC-LYw"/>
Expand Down
31 changes: 17 additions & 14 deletions Example/PullUpController/UI/MapViewController.swift
Expand Up @@ -19,8 +19,10 @@ class MapViewController: UIViewController {
}
@IBOutlet private weak var widthSlider: UISlider!
@IBOutlet private weak var initialStateSegmentedControl: UISegmentedControl!

private var originalPullUpControllerViewSize: CGSize = .zero
@IBOutlet weak var animateShowSwitch: UISwitch!
@IBOutlet weak var animateHideSwitch: UISwitch!

private func makeSearchViewControllerIfNeeded() -> SearchViewController {
let currentPullUpController = children
Expand All @@ -35,41 +37,42 @@ class MapViewController: UIViewController {
if originalPullUpControllerViewSize == .zero {
originalPullUpControllerViewSize = pullUpController.view.bounds.size
}

return pullUpController
}

override func viewDidLoad() {
super.viewDidLoad()
addPullUpController()
addPullUpController(animated: animateShowSwitch.isOn)
}
private func addPullUpController() {

private func addPullUpController(animated: Bool) {
let pullUpController = makeSearchViewControllerIfNeeded()
_ = pullUpController.view // call pullUpController.viewDidLoad()
addPullUpController(pullUpController,
initialStickyPointOffset: pullUpController.initialPointOffset,
animated: true)
animated: animated)
}

func zoom(to location: CLLocationCoordinate2D) {
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location, span: span)

mapView.setRegion(region, animated: true)
}

@IBAction private func addButtonTapped() {
guard
children.filter({ $0 is SearchViewController }).count == 0
else { return }
addPullUpController()
addPullUpController(animated: animateShowSwitch.isOn)
}

@IBAction private func removeButtonTapped() {
let pullUpController = makeSearchViewControllerIfNeeded()
removePullUpController(pullUpController, animated: true)
removePullUpController(pullUpController, animated: animateHideSwitch.isOn)
}

@IBAction private func widthSliderValueChanged(_ sender: UISlider) {
let pullUpController = makeSearchViewControllerIfNeeded()
let width = originalPullUpControllerViewSize.width * CGFloat(sender.value)
Expand All @@ -80,6 +83,6 @@ class MapViewController: UIViewController {
height: pullUpController.landscapeFrame.height))
pullUpController.updatePreferredFrameIfNeeded(animated: true)
}

}

45 changes: 28 additions & 17 deletions PullUpController/PullUpController.swift
Expand Up @@ -482,13 +482,17 @@ extension UIViewController {
assert(!(self is UITableViewController), "It's not possible to attach a PullUpController to a UITableViewController. Check this issue for more information: https://github.com/MarioIannotta/PullUpController/issues/14")
addChild(pullUpController)
pullUpController.setup(superview: view, initialStickyPointOffset: initialStickyPointOffset)
pullUpController.pullUpControllerAnimate(
action: .add,
withDuration: animated ? 0.3 : 0,
animations: { [weak self] in
self?.view.layoutIfNeeded()
},
completion: nil)
if animated {
pullUpController.pullUpControllerAnimate(
action: .add,
withDuration: 0.3,
animations: { [weak self] in
self?.view.layoutIfNeeded()
},
completion: nil)
} else {
view.layoutIfNeeded()
}
}

/**
Expand All @@ -498,17 +502,24 @@ extension UIViewController {
*/
open func removePullUpController(_ pullUpController: PullUpController, animated: Bool) {
pullUpController.hide()
pullUpController.pullUpControllerAnimate(
action: .remove,
withDuration: animated ? 0.3 : 0,
animations: { [weak self] in
self?.view.layoutIfNeeded()
},
completion: { _ in
pullUpController.willMove(toParent: nil)
pullUpController.view.removeFromSuperview()
pullUpController.removeFromParent()
if animated {
pullUpController.pullUpControllerAnimate(
action: .remove,
withDuration: 0.3,
animations: { [weak self] in
self?.view.layoutIfNeeded()
},
completion: { _ in
pullUpController.willMove(toParent: nil)
pullUpController.view.removeFromSuperview()
pullUpController.removeFromParent()
})
} else {
view.layoutIfNeeded()
pullUpController.willMove(toParent: nil)
pullUpController.view.removeFromSuperview()
pullUpController.removeFromParent()
}
}

}
Expand Down

0 comments on commit 1b29429

Please sign in to comment.