Skip to content

Commit

Permalink
Update to 1.7.1
Browse files Browse the repository at this point in the history
Add confirmation to dismiss by scroll. Change `SPStorkControllerConfirmDelegate` to required methods. Update example.
  • Loading branch information
ivanvorobei committed Jul 10, 2019
1 parent de779ae commit 9f4c1be
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 38 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions Example/stork-controller/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Controller: UIViewController {
let modal = ModalTableViewController()
let transitionDelegate = SPStorkTransitioningDelegate()
transitionDelegate.storkDelegate = self
transitionDelegate.confirmDelegate = modal
modal.transitioningDelegate = transitionDelegate
modal.modalPresentationStyle = .custom
self.present(modal, animated: true, completion: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import UIKit

@objc public protocol SPStorkControllerConfirmDelegate: class {

@objc optional var needConfirm: Bool { get }
var needConfirm: Bool { get }

@objc optional func confirm(_ completion: @escaping (_ isConfirmed: Bool)->())
func confirm(_ completion: @escaping (_ isConfirmed: Bool)->())
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,35 @@ public enum SPStorkController {
scrollView.subviews.forEach {
$0.transform = CGAffineTransform(translationX: 0, y: -translation)
}
/* Maybe migrate to it in future. Bug with bottom safe area
scrollView.transform = CGAffineTransform(translationX: 0, y: -translation)
scrollView.scrollIndicatorInsets.top = (indicatorInset ?? 0) + translation
*/
presentationController.setIndicator(style: scrollView.isTracking ? .line : .arrow)
if translation >= presentationController.translateForDismiss * 0.4 {
if !scrollView.isTracking && !scrollView.isDragging {
presentationController.presentedViewController.dismiss(animated: true, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})

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()
}

return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ 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
private var afterReleaseDismissing: Bool = false
Expand All @@ -69,7 +70,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
return factor
}

private var feedbackGenerator: UIImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
private var feedbackGenerator = UIImpactFeedbackGenerator(style: .light)

override var presentedView: UIView? {
let view = self.presentedViewController.view
Expand Down Expand Up @@ -358,9 +359,20 @@ extension SPStorkPresentationController {
}

if translation >= self.translateForDismiss {
if self.confirmDelegate?.needConfirm ?? false {

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

if self.workConfirmation { return }

if confirmDelegate.needConfirm {
returnToDefault()
self.confirmDelegate?.confirm?({ (isConfirmed) in
self.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
self.workConfirmation = false
self.afterReleaseDismissing = false
if isConfirmed {
dismissBySwipe()
}
Expand Down Expand Up @@ -446,8 +458,10 @@ extension SPStorkPresentationController {
let afterRealseDismissing = (translation >= self.translateForDismiss)
if afterRealseDismissing != self.afterReleaseDismissing {
self.afterReleaseDismissing = afterRealseDismissing
if self.hapticMoments.contains(.willDismissIfRelease) {
self.feedbackGenerator.impactOccurred()
if !self.workConfirmation {
if self.hapticMoments.contains(.willDismissIfRelease) {
self.feedbackGenerator.impactOccurred()
}
}
}
}
Expand Down Expand Up @@ -479,8 +493,6 @@ extension SPStorkPresentationController {
private func updateLayoutIndicator() {
self.indicatorView.style = .line
self.indicatorView.sizeToFit()
//self.indicatorView.frame.origin.y = 12
//self.indicatorView.center.x = presentedView.frame.width / 2
}

private func updateLayoutCloseButton() {
Expand Down
18 changes: 18 additions & 0 deletions Example/stork-controller/ModalTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ extension ModalTableViewController: UITableViewDelegate {
}
}

extension ModalTableViewController: SPStorkControllerConfirmDelegate {

var needConfirm: Bool {
return true
}

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)
})
alertController.addCancelAction(title: "Cancel") {
completion(false)
}
self.present(alertController)
}
}
2 changes: 1 addition & 1 deletion Example/stork-controller/ModalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension ModalViewController: SPStorkControllerConfirmDelegate {

func confirm(_ completion: @escaping (Bool) -> ()) {
let alertController = UIAlertController(title: "Need dismiss?", message: "It test confirm option for SPStorkController", preferredStyle: .actionSheet)
alertController.addAction(title: "Confirm", complection: {
alertController.addDestructiveAction(title: "Confirm", complection: {
completion(true)
})
alertController.addCancelAction(title: "Cancel") {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ For confirm closing by swipe, use `SPStorkControllerConfirmDelegate`. Implenet p
```swift
@objc public protocol SPStorkControllerConfirmDelegate: class {

@objc optional var needConfirm: Bool { get }
var needConfirm: Bool { get }

@objc optional func confirm(_ completion: @escaping (_ isConfirmed: Bool)->())
func confirm(_ completion: @escaping (_ isConfirmed: 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"
s.version = "1.7.1"
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import UIKit

@objc public protocol SPStorkControllerConfirmDelegate: class {

@objc optional var needConfirm: Bool { get }
var needConfirm: Bool { get }

@objc optional func confirm(_ completion: @escaping (_ isConfirmed: Bool)->())
func confirm(_ completion: @escaping (_ isConfirmed: Bool)->())
}
33 changes: 26 additions & 7 deletions Source/SPStorkController/SPStorkController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,35 @@ public enum SPStorkController {
scrollView.subviews.forEach {
$0.transform = CGAffineTransform(translationX: 0, y: -translation)
}
/* Maybe migrate to it in future. Bug with bottom safe area
scrollView.transform = CGAffineTransform(translationX: 0, y: -translation)
scrollView.scrollIndicatorInsets.top = (indicatorInset ?? 0) + translation
*/
presentationController.setIndicator(style: scrollView.isTracking ? .line : .arrow)
if translation >= presentationController.translateForDismiss * 0.4 {
if !scrollView.isTracking && !scrollView.isDragging {
presentationController.presentedViewController.dismiss(animated: true, completion: {
presentationController.storkDelegate?.didDismissStorkBySwipe?()
})

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()
}

return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ 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
private var afterReleaseDismissing: Bool = false
Expand All @@ -69,7 +70,7 @@ class SPStorkPresentationController: UIPresentationController, UIGestureRecogniz
return factor
}

private var feedbackGenerator: UIImpactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
private var feedbackGenerator = UIImpactFeedbackGenerator(style: .light)

override var presentedView: UIView? {
let view = self.presentedViewController.view
Expand Down Expand Up @@ -358,9 +359,20 @@ extension SPStorkPresentationController {
}

if translation >= self.translateForDismiss {
if self.confirmDelegate?.needConfirm ?? false {

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

if self.workConfirmation { return }

if confirmDelegate.needConfirm {
returnToDefault()
self.confirmDelegate?.confirm?({ (isConfirmed) in
self.workConfirmation = true
confirmDelegate.confirm({ (isConfirmed) in
self.workConfirmation = false
self.afterReleaseDismissing = false
if isConfirmed {
dismissBySwipe()
}
Expand Down Expand Up @@ -446,8 +458,10 @@ extension SPStorkPresentationController {
let afterRealseDismissing = (translation >= self.translateForDismiss)
if afterRealseDismissing != self.afterReleaseDismissing {
self.afterReleaseDismissing = afterRealseDismissing
if self.hapticMoments.contains(.willDismissIfRelease) {
self.feedbackGenerator.impactOccurred()
if !self.workConfirmation {
if self.hapticMoments.contains(.willDismissIfRelease) {
self.feedbackGenerator.impactOccurred()
}
}
}
}
Expand Down Expand Up @@ -479,8 +493,6 @@ extension SPStorkPresentationController {
private func updateLayoutIndicator() {
self.indicatorView.style = .line
self.indicatorView.sizeToFit()
//self.indicatorView.frame.origin.y = 12
//self.indicatorView.center.x = presentedView.frame.width / 2
}

private func updateLayoutCloseButton() {
Expand Down

0 comments on commit 9f4c1be

Please sign in to comment.