Skip to content

Commit 7f4262b

Browse files
committed
Release 1.3.4
- Added applyAnimatedTranslation function which allows you to apply simulated dragging. This can be used when simulating a scroll. - Exposed the UIPanGestureRecognizer object of the main frame allow delegation control externally.
1 parent d0c9fb7 commit 7f4262b

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-8
lines changed

AZDialogView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "AZDialogView"
3-
s.version = "1.3.3"
3+
s.version = "1.3.4"
44
s.summary = "A highly customizable alert dialog controller that mimics Snapchat's alert dialog."
55
s.homepage = "https://github.com/Minitour/AZDialogViewController"
66
s.license = "MIT"

AZDialogViewControllerExample/AZDialogViewControllerExample/ViewController.swift

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class ViewController: UIViewController {
140140
return true
141141
}
142142

143-
dialogController.addAction(AZDialogAction(title: "Subscribe", handler: { [weak self] (dialog) -> (Void) in
143+
dialogController.addAction(AZDialogAction(title: "Subscribe", handler: { (dialog) -> (Void) in
144144
//dialog.title = "title"
145145
//dialog.message = "new message"
146146
//dialog.image = dialog.image == nil ? #imageLiteral(resourceName: "ign") : nil
@@ -348,10 +348,13 @@ class ViewController: UIViewController {
348348

349349
dialogController.show(in: self)
350350
}
351-
351+
352+
var tableViewDialogController: AZDialogViewController?
353+
352354
func tableViewDialog(){
353355
let dialog = AZDialogViewController(title: "Switch Account", message: nil,widthRatio: 1.0)
354-
356+
tableViewDialogController = dialog
357+
355358
dialog.showSeparator = false
356359

357360
let container = dialog.container
@@ -367,6 +370,7 @@ class ViewController: UIViewController {
367370
tableView.delegate = self
368371
tableView.dataSource = self
369372
tableView.separatorColor = .clear
373+
//tableView.bouncesZoom = false
370374
tableView.bounces = false
371375

372376
tableView.translatesAutoresizingMaskIntoConstraints = false
@@ -375,6 +379,9 @@ class ViewController: UIViewController {
375379
tableView.leftAnchor.constraint(equalTo: container.leftAnchor).isActive = true
376380
tableView.rightAnchor.constraint(equalTo: container.rightAnchor).isActive = true
377381

382+
dialog.gestureRecognizer.delegate = self
383+
dialog.dismissDirection = .bottom
384+
378385
dialog.show(in: self) { dialog in
379386
dialog.contentOffset = self.view.frame.height / 2.0 - dialog.estimatedHeight / 2.0 + 15
380387
}
@@ -409,6 +416,9 @@ class ViewController: UIViewController {
409416
}
410417

411418
}
419+
420+
var shouldDismiss: Bool = false
421+
var velocity: CGFloat = 0.0
412422

413423
}
414424

@@ -417,7 +427,49 @@ class ViewController: UIViewController {
417427
extension ViewController: UITableViewDelegate{
418428
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
419429
tableView.deselectRow(at: indexPath, animated: true)
420-
dismiss(animated: true, completion: nil)
430+
tableViewDialogController?.dismiss()
431+
}
432+
433+
434+
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
435+
let duration = Double(1.0/abs(velocity))
436+
if scrollView.isAtTop {
437+
if shouldDismiss {
438+
tableViewDialogController?.animationDuration = duration
439+
tableViewDialogController?.dismiss()
440+
}else {
441+
tableViewDialogController?.applyAnimatedTranslation(-velocity * 35.0,duration: min(max(duration,0.2),0.4))
442+
}
443+
444+
}
445+
}
446+
447+
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
448+
shouldDismiss = velocity.y < -3.0
449+
self.velocity = velocity.y
450+
}
451+
452+
func scrollViewDidScroll(_ scrollView: UIScrollView) {
453+
scrollView.bounces = !scrollView.isAtTop
454+
455+
}
456+
}
457+
458+
extension ViewController: UIGestureRecognizerDelegate {
459+
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
460+
461+
let optionalTableView: UITableView? = otherGestureRecognizer.view as? UITableView
462+
463+
guard let tableView = optionalTableView,
464+
let panGesture = gestureRecognizer as? UIPanGestureRecognizer,
465+
let direction = panGesture.direction
466+
else { return false }
467+
468+
if tableView.isAtTop && direction == .down {
469+
return true
470+
} else {
471+
return false
472+
}
421473
}
422474
}
423475

@@ -498,3 +550,49 @@ class HighlightableButton: UIButton{
498550
}
499551

500552

553+
public extension UIScrollView {
554+
555+
var isAtTop: Bool {
556+
return contentOffset.y <= verticalOffsetForTop
557+
}
558+
559+
var isAtBottom: Bool {
560+
return contentOffset.y >= verticalOffsetForBottom
561+
}
562+
563+
var verticalOffsetForTop: CGFloat {
564+
let topInset = contentInset.top
565+
return -topInset
566+
}
567+
568+
var verticalOffsetForBottom: CGFloat {
569+
let scrollViewHeight = bounds.height
570+
let scrollContentSizeHeight = contentSize.height
571+
let bottomInset = contentInset.bottom
572+
let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight
573+
return scrollViewBottomOffset
574+
}
575+
576+
}
577+
578+
public enum PanDirection: Int {
579+
case up, down, left, right
580+
public var isVertical: Bool { return [.up, .down].contains(self) }
581+
public var isHorizontal: Bool { return !isVertical }
582+
}
583+
584+
public extension UIPanGestureRecognizer {
585+
586+
public var direction: PanDirection? {
587+
let velocity = self.velocity(in: view)
588+
let isVertical = abs(velocity.y) > abs(velocity.x)
589+
switch (isVertical, velocity.x, velocity.y) {
590+
case (true, _, let y) where y < 0: return .up
591+
case (true, _, let y) where y > 0: return .down
592+
case (false, let x, _) where x > 0: return .right
593+
case (false, let x, _) where x < 0: return .left
594+
default: return nil
595+
}
596+
}
597+
598+
}

Sources/AZDialogViewController.swift

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ open class AZDialogViewController: UIViewController{
212212
@objc
213213
open fileprivate(set) lazy var blurView: UIVisualEffectView = UIVisualEffectView()
214214

215+
@objc fileprivate(set) lazy var gestureRecognizer = UIPanGestureRecognizer(
216+
target: self,
217+
action: #selector(AZDialogViewController.handlePanGesture(_:))
218+
)
219+
215220
@objc
216221
open var dialogView: UIView? {
217222
return baseView
@@ -306,6 +311,7 @@ open class AZDialogViewController: UIViewController{
306311
UIView.animate(withDuration: animationDuration){
307312
baseView.center = CGPoint(x: center.x ,y: center.y + offset)
308313
}
314+
baseView.lastLocation = baseView.center
309315
}
310316
}
311317
}
@@ -615,6 +621,11 @@ open class AZDialogViewController: UIViewController{
615621
controller.present(self, animated: false,completion: nil)
616622
}
617623

624+
625+
/// Use this function to add additional sections to an existing dialog. Note that you can use this function only when the dialog has already appeared.
626+
///
627+
/// - Parameter view: The view you wish to add.
628+
/// - Returns: Self.
618629
@objc
619630
@discardableResult
620631
open func section(view: UIView) -> Self {
@@ -656,6 +667,10 @@ open class AZDialogViewController: UIViewController{
656667
return self
657668
}
658669

670+
671+
/// Use this function to remove a section at a certain index.
672+
///
673+
/// - Parameter index: The index of the section.
659674
open func removeSection(_ index: Int) {
660675
guard let baseView = baseView else { return }
661676

@@ -665,6 +680,28 @@ open class AZDialogViewController: UIViewController{
665680
sections[index].removeFromSuperview()
666681
}
667682
}
683+
684+
685+
/// This function applies translation to the dialog with animation and then returns it to its original position.
686+
///
687+
/// - Parameters:
688+
/// - translation: The translation value.
689+
/// - duration: The duration of the overall animation.
690+
open func applyAnimatedTranslation(_ translation: CGFloat, duration: TimeInterval = 0.35){
691+
692+
let yTranslation = baseView.lastLocation.y + translation
693+
694+
UIView.animate(withDuration: duration / 2.0, animations: { [weak self] in
695+
guard let `self` = self else { return }
696+
self.baseView.center = CGPoint(x: self.baseView.lastLocation.x , y: yTranslation)
697+
}) { (completion) in
698+
var point = self.view.center
699+
point.y = point.y + self.contentOffset
700+
UIView.animate(withDuration: duration / 2.0) {
701+
[weak self] () -> Void in self?.baseView.center = point
702+
}
703+
}
704+
}
668705

669706
//MARK: - Overriding methods
670707

@@ -778,7 +815,7 @@ open class AZDialogViewController: UIViewController{
778815
createGesutre(for: baseView)
779816
createGesutre(for: container)
780817

781-
baseView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(AZDialogViewController.handlePanGesture(_:))))
818+
baseView.addGestureRecognizer(gestureRecognizer)
782819

783820
baseView.layer.cornerRadius = 15
784821
baseView.layer.backgroundColor = alertBackgroundColor?.cgColor ?? UIColor.white.cgColor
@@ -878,12 +915,11 @@ open class AZDialogViewController: UIViewController{
878915
self.widthRatio = widthRatio
879916
}
880917

881-
882918
/// Selector method - used to handle the dragging.
883919
///
884920
/// - Parameter sender: The Gesture Recognizer.
885921
@objc internal func handlePanGesture(_ sender: UIPanGestureRecognizer){
886-
922+
887923
//if panning is disabled return
888924
if !allowDragGesture{
889925
return

0 commit comments

Comments
 (0)