Skip to content

Commit

Permalink
Update to 1.7.2
Browse files Browse the repository at this point in the history
Fix bugs with events delegate.
  • Loading branch information
ivanvorobei committed Jul 10, 2019
1 parent 9f4c1be commit 8348cd6
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 141 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,9 @@ public enum SPStorkController {
presentationController.setIndicator(style: scrollView.isTracking ? .line : .arrow)
if translation >= presentationController.translateForDismiss * 0.4 {
if !scrollView.isTracking && !scrollView.isDragging {

let dismiss = {
presentationController.presentedViewController.dismiss(animated: true, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})
}

guard let confirmDelegate = presentationController.confirmDelegate else {
dismiss()
return
}

if presentationController.workConfirmation { return }

if confirmDelegate.needConfirm {
presentationController.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
presentationController.workConfirmation = false
if isConfirmed {
dismiss()
}
})
} else {
dismiss()
}

presentationController.dismissWithConfirmation(prepare: nil, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
private var snapshotViewTopConstraint: NSLayoutConstraint?
private var snapshotViewWidthConstraint: NSLayoutConstraint?
private var snapshotViewAspectRatioConstraint: NSLayoutConstraint?

var workConfirmation: Bool = false
private var workGester: Bool = false
private var startDismissing: Bool = false
Expand Down Expand Up @@ -107,7 +107,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz

if self.showIndicator {
self.indicatorView.color = self.indicatorColor
let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.dismissAction))
let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.tapIndicator))
tap.cancelsTouchesInView = false
self.indicatorView.addGestureRecognizer(tap)
presentedView.addSubview(self.indicatorView)
Expand All @@ -122,7 +122,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
self.gradeView.alpha = 0

if self.showCloseButton {
self.closeButton.addTarget(self, action: #selector(self.dismissAction), for: .touchUpInside)
self.closeButton.addTarget(self, action: #selector(self.tapCloseButton), for: .touchUpInside)
presentedView.addSubview(self.closeButton)
}
self.updateLayoutCloseButton()
Expand Down Expand Up @@ -209,7 +209,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
self.updateSnapshotAspectRatio()

if self.tapAroundToDismissEnabled {
self.tap = UITapGestureRecognizer.init(target: self, action: #selector(self.dismissAction))
self.tap = UITapGestureRecognizer.init(target: self, action: #selector(self.tapArround))
self.tap?.cancelsTouchesInView = false
self.snapshotViewContainer.addGestureRecognizer(self.tap!)
}
Expand All @@ -223,21 +223,13 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
}
}

@objc func dismissAction() {
self.presentingViewController.view.endEditing(true)
self.presentedViewController.view.endEditing(true)
self.presentedViewController.dismiss(animated: true, completion: {
self.storkDelegate?.didDismissStorkByTap?()
})
}

override func dismissalTransitionWillBegin() {
super.dismissalTransitionWillBegin()
guard let containerView = containerView else { return }
self.startDismissing = true

let initialFrame: CGRect = presentingViewController.isPresentedAsStork ? presentingViewController.view.frame : containerView.bounds

let initialTransform = CGAffineTransform.identity
.translatedBy(x: 0, y: -initialFrame.origin.y)
.translatedBy(x: 0, y: self.topSpace)
Expand Down Expand Up @@ -314,6 +306,56 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz

extension SPStorkPresentationController {

@objc func tapIndicator() {
self.dismissWithConfirmation(prepare: nil, completion: {
self.storkDelegate?.didDismissStorkByTap?()
})
}

@objc func tapArround() {
self.dismissWithConfirmation(prepare: nil, completion: {
self.storkDelegate?.didDismissStorkByTap?()
})
}

@objc func tapCloseButton() {
self.dismissWithConfirmation(prepare: nil, completion: {
self.storkDelegate?.didDismissStorkByTap?()
})
}

public func dismissWithConfirmation(prepare: (()->())?, completion: (()->())?) {

let dismiss = {
self.presentingViewController.view.endEditing(true)
self.presentedViewController.view.endEditing(true)
self.presentedViewController.dismiss(animated: true, completion: {
completion?()
})
}

guard let confirmDelegate = self.confirmDelegate else {
dismiss()
return
}

if self.workConfirmation { return }

if confirmDelegate.needConfirm {
prepare?()
self.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
self.workConfirmation = false
self.afterReleaseDismissing = false
if isConfirmed {
dismiss()
}
})
} else {
dismiss()
}
}

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

Expand All @@ -337,7 +379,7 @@ extension SPStorkPresentationController {
self.workGester = false
let translation = gestureRecognizer.translation(in: presentedView).y

let returnToDefault = {
let toDefault = {
self.indicatorView.style = .arrow
UIView.animate(
withDuration: 0.6,
Expand All @@ -352,36 +394,12 @@ extension SPStorkPresentationController {
})
}

let dismissBySwipe = {
self.presentedViewController.dismiss(animated: true, completion: {
if translation >= self.translateForDismiss {
self.dismissWithConfirmation(prepare: toDefault, completion: {
self.storkDelegate?.didDismissStorkBySwipe?()
})
}

if translation >= self.translateForDismiss {

guard let confirmDelegate = self.confirmDelegate else {
dismissBySwipe()
return
}

if self.workConfirmation { return }

if confirmDelegate.needConfirm {
returnToDefault()
self.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
self.workConfirmation = false
self.afterReleaseDismissing = false
if isConfirmed {
dismissBySwipe()
}
})
} else {
dismissBySwipe()
}
} else {
returnToDefault()
toDefault()
}
default:
break
Expand Down Expand Up @@ -432,7 +450,7 @@ extension SPStorkPresentationController {

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

if translation >= 0 {
let translationForModal: CGFloat = {
if translation >= elasticThreshold {
Expand Down
7 changes: 5 additions & 2 deletions Example/stork-controller/ModalTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class ModalTableViewController: UIViewController {
}

@objc func dismissAction() {
self.dismiss()
if let storkPresentationController = self.presentationController as? SPStorkPresentationController {
storkPresentationController.dismissWithConfirmation(prepare: nil, completion: {
print("Custom completion for confirmation. Confirmation is optional.")
})
}
}
}

Expand Down Expand Up @@ -86,7 +90,6 @@ extension ModalTableViewController: SPStorkControllerConfirmDelegate {
}

func confirm(_ completion: @escaping (Bool) -> ()) {
print("confirm")
let alertController = UIAlertController(title: "Need dismiss?", message: "It test confirm option for SPStorkController", preferredStyle: .actionSheet)
alertController.addDestructiveAction(title: "Confirm", complection: {
completion(true)
Expand Down
8 changes: 6 additions & 2 deletions Example/stork-controller/ModalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class ModalViewController: UIViewController {
}

@objc func dismissAction() {
self.dismiss()
if let storkPresentationController = self.presentationController as? SPStorkPresentationController {
storkPresentationController.dismissWithConfirmation(prepare: nil, completion: {
print("Custom completion for confirmation. Confirmation is optional.")
})
}
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -35,7 +39,7 @@ class ModalViewController: UIViewController {
extension ModalViewController: SPStorkControllerConfirmDelegate {

var needConfirm: Bool {
return false
return true
}

func confirm(_ completion: @escaping (Bool) -> ()) {
Expand Down
2 changes: 1 addition & 1 deletion SPStorkController.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SPStorkController"
s.version = "1.7.1"
s.version = "1.7.2"
s.summary = "Very similar to the controllers displayed in Apple Music, Podcasts and Mail Apple's applications."
s.homepage = "https://github.com/IvanVorobei/SPStorkController"
s.source = { :git => "https://github.com/IvanVorobei/SPStorkController.git", :tag => s.version }
Expand Down
29 changes: 3 additions & 26 deletions Source/SPStorkController/SPStorkController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,9 @@ public enum SPStorkController {
presentationController.setIndicator(style: scrollView.isTracking ? .line : .arrow)
if translation >= presentationController.translateForDismiss * 0.4 {
if !scrollView.isTracking && !scrollView.isDragging {

let dismiss = {
presentationController.presentedViewController.dismiss(animated: true, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})
}

guard let confirmDelegate = presentationController.confirmDelegate else {
dismiss()
return
}

if presentationController.workConfirmation { return }

if confirmDelegate.needConfirm {
presentationController.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
presentationController.workConfirmation = false
if isConfirmed {
dismiss()
}
})
} else {
dismiss()
}

presentationController.dismissWithConfirmation(prepare: nil, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})
return
}
}
Expand Down
Loading

0 comments on commit 8348cd6

Please sign in to comment.