Skip to content

Commit

Permalink
Added support for haptic feedback when interacting with the popup.
Browse files Browse the repository at this point in the history
  • Loading branch information
iDevelopper committed Dec 14, 2023
1 parent cc2ddff commit 70e6f7a
Showing 1 changed file with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,22 @@ extension PBPopupPresentationStyle
The status bar style of the popup content view controller. Return this value when you override the preferredStatusBarStyle variable.
*/
@objc public var popupStatusBarStyle: UIStatusBarStyle = .default


/**
Controls whether interaction with the popup generates haptic feedback to the user. The default value is `true`.
*/
@objc public var allowPopupHapticFeedbackGeneration: Bool = true

/**
The soft soimpact feedback specific intensity. A value between 0.0 and 1.0.
*/
@objc public var softFeedbackGeneratorIntensity: CGFloat = 0.6

/**
The rigid impact feedback specific intensity. A value between 0.0 and 1.0.
*/
@objc public var rigidFeedbackGeneratorIntensity: CGFloat = 0.7

// MARK: - Private Properties

@objc internal weak var containerViewController: UIViewController!
Expand Down Expand Up @@ -460,6 +475,9 @@ extension PBPopupPresentationStyle
internal var popupPresentationInteractiveController: PBPopupInteractivePresentationController!
internal var popupDismissalInteractiveController: PBPopupInteractivePresentationController!

private var softFeedbackGenerator: UIImpactFeedbackGenerator!
private var rigidFeedbackGenerator: UIImpactFeedbackGenerator!

private weak var previewingContext: UIViewControllerPreviewing?

internal func dropShadowViewFor(_ view: UIView) -> UIView?
Expand Down Expand Up @@ -528,6 +546,9 @@ extension PBPopupPresentationStyle

self.popupPresentationState = .hidden
self.containerViewController = controller

self.softFeedbackGenerator = UIImpactFeedbackGenerator(style: .soft)
self.rigidFeedbackGenerator = UIImpactFeedbackGenerator(style: .rigid)
}

required init?(coder aDecoder: NSCoder)
Expand Down Expand Up @@ -616,6 +637,40 @@ extension PBPopupPresentationStyle
return rv
}

// MARK: - Popup Bar Haptic Feedback Generation

private func _prepareSoftFeedback()
{
if self.allowPopupHapticFeedbackGeneration == false {
return
}
self.softFeedbackGenerator.prepare()
}

private func _generateSoftFeedbackWithIntensity(_ intensity: CGFloat)
{
if self.allowPopupHapticFeedbackGeneration == false {
return
}
self.softFeedbackGenerator.impactOccurred(intensity: intensity)
}

private func _prepareRigidFeedback()
{
if self.allowPopupHapticFeedbackGeneration == false {
return
}
self.rigidFeedbackGenerator.prepare()
}

private func _generateRigidFeedbackWithIntensity(_ intensity: CGFloat)
{
if self.allowPopupHapticFeedbackGeneration == false {
return
}
self.rigidFeedbackGenerator.impactOccurred(intensity: intensity)
}

// MARK: - Popup Bar Animation

internal func _presentPopupBarAnimated(_ animated: Bool, completionBlock: (() -> Swift.Void)? = nil)
Expand Down Expand Up @@ -790,13 +845,18 @@ extension PBPopupPresentationStyle

@objc internal func popupTapGestureRecognized(tgr: UITapGestureRecognizer)
{
self._prepareRigidFeedback()

if self.delegate?.popupControllerTapGestureShouldBegin?(self, state: self.popupPresentationState) == false {
return
}
if let vc = self.containerViewController {
if self.delegate?.popupController?(self, shouldOpen: vc.popupContentViewController) == false {
return
}

self._generateRigidFeedbackWithIntensity(self.rigidFeedbackGeneratorIntensity)

vc.popupBar.setHighlighted(true, animated: false)
self._openPopupAnimated(true) {
vc.popupBar.setHighlighted(false, animated: false)
Expand Down Expand Up @@ -843,6 +903,7 @@ extension PBPopupPresentationStyle

@objc internal func closePopupContent()
{
self._prepareRigidFeedback()
// The popup content has a popup open. Close it before closing the main popup content.
if let popupController = UIViewController.getAssociatedPopupControllerFor(self.containerViewController.popupContentViewController) {
if popupController.popupPresentationState == .open {
Expand All @@ -858,6 +919,7 @@ extension PBPopupPresentationStyle
else {
// Close popup content.
if self.popupPresentationState == .open {
self._generateRigidFeedbackWithIntensity(self.rigidFeedbackGeneratorIntensity)
self.closePopupContent(animated: true)
}
}
Expand Down Expand Up @@ -1143,6 +1205,9 @@ extension PBPopupController: PBPopupInteractivePresentationDelegate
{
if let vc = self.containerViewController {
self.popupContentPanGestureRecognizer.isEnabled = false

self._prepareSoftFeedback()

if self.delegate?.popupController?(self, shouldOpen: vc.popupContentViewController) == false {
return
}
Expand All @@ -1159,6 +1224,8 @@ extension PBPopupController: PBPopupInteractivePresentationDelegate
}
}

self._generateSoftFeedbackWithIntensity(self.softFeedbackGeneratorIntensity)

vc.present(vc.popupContentViewController, animated: true) {
if self.popupPresentationState == .opening {
if let scrollView = vc.popupContentViewController.view as? UIScrollView {
Expand Down Expand Up @@ -1189,13 +1256,18 @@ extension PBPopupController: PBPopupInteractivePresentationDelegate
if self.delegate?.popupController?(self, shouldClose: vc.popupContentViewController) == false {
return
}

self._prepareSoftFeedback()

vc.view.setNeedsLayout()
vc.view.layoutIfNeeded()

let previousState = self.popupPresentationState
self.popupPresentationState = .transitioning
self.delegate?.popupController?(self, stateChanged: self.popupPresentationState, previousState: previousState)

self._generateSoftFeedbackWithIntensity(self.softFeedbackGeneratorIntensity)

vc.popupContentViewController.dismiss(animated: true) {
if self.popupPresentationState == .closing {
if let scrollView = vc.popupContentViewController.view as? UIScrollView {
Expand Down

0 comments on commit 70e6f7a

Please sign in to comment.