Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	Presentr/PresentrController.swift
  • Loading branch information
danlozano committed Nov 2, 2016
2 parents 6b8eedf + 68ac0fb commit 1e0155a
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### What's New

#### 1.0.1
- Added keyboard observation options for .popup (thanks @aasatt)
- Swipe to dismiss gesture (thanks @josejuanqm)

#### 1.0.0
- Adds support for Swift 3.0, XCode 8 & iOS 10.
- This will be the only maintained version from here on out.
Expand Down
6 changes: 6 additions & 0 deletions Presentr/ModalSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import Foundation
- Half: Half of the screen.
- Full: Full screen.
- Custom: Custom fixed size.
- Fluid: Custom percentage-based fluid size.
*/
public enum ModalSize {

case `default`
case half
case full
case custom(size: Float)
case fluid(percentage: Float)

/**
Calculates the exact width value for the presented view controller.
Expand All @@ -40,6 +42,8 @@ public enum ModalSize {
return Float(parentSize.width)
case .custom(let size):
return size
case .fluid(let percentage):
return floorf(Float(parentSize.width) * percentage)
}
}

Expand All @@ -60,6 +64,8 @@ public enum ModalSize {
return Float(parentSize.height)
case .custom(let size):
return size
case .fluid(let percentage):
return floorf(Float(parentSize.height) * percentage)
}
}

Expand Down
43 changes: 33 additions & 10 deletions Presentr/Presentr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ struct PresentrConstants {
}
}

/// Helper struct that represents the shadow properties
public struct PresentrShadow {

public let shadowColor: UIColor?
public let shadowOpacity: Float?
public let shadowOffset: CGSize?
public let shadowRadius: CGFloat?

public init(shadowColor: UIColor?, shadowOpacity: Float?, shadowOffset: CGSize?, shadowRadius: CGFloat?) {
self.shadowColor = shadowColor
self.shadowOpacity = shadowOpacity
self.shadowOffset = shadowOffset
self.shadowRadius = shadowRadius
}

}

// MARK: - PresentrDelegate

/**
Expand Down Expand Up @@ -56,6 +73,12 @@ public class Presentr: NSObject {
/// Should the presented controller have rounded corners. Default is true, except for .BottomHalf and .TopHalf presentation types.
public var roundCorners = true

/// Radius of rounded corners if roundCorners is true. Default is 4.
public var cornerRadius: CGFloat = 4

/// Radius of rounded corners if roundCorners is true. Default is 4.
public var dropShadow: PresentrShadow?

/// Should the presented controller dismiss on background tap. Default is true.
public var dismissOnTap = true

Expand All @@ -80,16 +103,6 @@ public class Presentr: NSObject {
/// How the presented view controller should respond to keyboard presentation.
public var keyboardTranslationType: KeyboardTranslationType = .none

// MARK: Private Helper Properties

fileprivate var transitionForPresent: TransitionType {
return transitionType ?? presentationType.defaultTransitionType()
}

fileprivate var transitionForDismiss: TransitionType {
return dismissTransitionType ?? transitionType ?? presentationType.defaultTransitionType()
}

// MARK: Init

public init(presentationType: PresentationType) {
Expand Down Expand Up @@ -140,6 +153,14 @@ public class Presentr: NSObject {

}

fileprivate var transitionForPresent: TransitionType {
return transitionType ?? presentationType.defaultTransitionType()
}

fileprivate var transitionForDismiss: TransitionType {
return dismissTransitionType ?? transitionType ?? presentationType.defaultTransitionType()
}

}

// MARK: - UIViewControllerTransitioningDelegate
Expand All @@ -166,6 +187,8 @@ extension Presentr: UIViewControllerTransitioningDelegate {
presentingViewController: presenting,
presentationType: presentationType,
roundCorners: roundCorners,
cornerRadius: cornerRadius,
dropShadow: dropShadow,
dismissOnTap: dismissOnTap,
dismissOnSwipe: dismissOnSwipe,
backgroundColor: backgroundColor,
Expand Down
47 changes: 43 additions & 4 deletions Presentr/PresentrController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class PresentrController: UIPresentationController, UIAdaptivePresentationContro

/// Should the presented controller's view have rounded corners.
let roundCorners: Bool

/// Radius of rounded corners if roundCorners is true.
let cornerRadius: CGFloat

/// Shadow settings
let dropShadow: PresentrShadow?

/// Should the presented controller dismiss on background tap.
let dismissOnTap: Bool
Expand Down Expand Up @@ -57,13 +63,16 @@ class PresentrController: UIPresentationController, UIAdaptivePresentationContro
fileprivate var chromeView = UIView()
fileprivate var keyboardIsShowing: Bool = false
private var translationStart: CGPoint = CGPoint.zero

private var presentedViewIsBeingDissmissed: Bool = false

// MARK: Init
init(presentedViewController: UIViewController,
presentingViewController: UIViewController?,
presentationType: PresentationType,
roundCorners: Bool,
cornerRadius: CGFloat,
dropShadow: PresentrShadow?,
dismissOnTap: Bool,
dismissOnSwipe: Bool,
backgroundColor: UIColor,
Expand All @@ -75,6 +84,8 @@ class PresentrController: UIPresentationController, UIAdaptivePresentationContro

self.presentationType = presentationType
self.roundCorners = roundCorners
self.cornerRadius = cornerRadius
self.dropShadow = dropShadow
self.dismissOnTap = dismissOnTap
self.dismissOnSwipe = dismissOnSwipe
self.keyboardTranslationType = keyboardTranslationType
Expand All @@ -89,7 +100,13 @@ class PresentrController: UIPresentationController, UIAdaptivePresentationContro
} else {
removeCornerRadiusFromPresentedView()
}


if dropShadow != nil {
addDropShadowToPresentedView()
} else {
removeDropShadowFromPresentedView()
}

if dismissOnSwipe {
setupDismissOnSwipe()
}
Expand Down Expand Up @@ -120,14 +137,36 @@ class PresentrController: UIPresentationController, UIAdaptivePresentationContro
}

private func addCornerRadiusToPresentedView() {
presentedViewController.view.layer.cornerRadius = 4
presentedViewController.view.layer.cornerRadius = cornerRadius
presentedViewController.view.layer.masksToBounds = true
}

private func removeCornerRadiusFromPresentedView() {
presentedViewController.view.layer.cornerRadius = 0
}


private func addDropShadowToPresentedView() {
guard let shadow = self.dropShadow else { return }
presentedViewController.view.layer.masksToBounds = false
if let shadowColor = shadow.shadowColor?.cgColor {
presentedViewController.view.layer.shadowColor = shadowColor
}
if let shadowOpacity = shadow.shadowOpacity {
presentedViewController.view.layer.shadowOpacity = shadowOpacity
}
if let shadowOffset = shadow.shadowOffset {
presentedViewController.view.layer.shadowOffset = shadowOffset
}
if let shadowRadius = shadow.shadowRadius {
presentedViewController.view.layer.shadowRadius = shadowRadius
}
}

private func removeDropShadowFromPresentedView() {
presentedViewController.view.layer.masksToBounds = true
presentedViewController.view.layer.shadowOpacity = 0
}

private func registerKeyboardObserver() {
NotificationCenter.default.addObserver(self, selector: #selector(PresentrController.keyboardWasShown(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(PresentrController.keyboardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
Expand Down Expand Up @@ -292,7 +331,7 @@ extension PresentrController {
chromeView.frame = containerView!.bounds
presentedView!.frame = frameOfPresentedViewInContainerView
}

// MARK: Animation

override func presentationTransitionWillBegin() {
Expand Down
Loading

0 comments on commit 1e0155a

Please sign in to comment.