Skip to content

Commit

Permalink
Release 1.3.4
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Minitour committed Mar 16, 2019
1 parent d0c9fb7 commit 7f4262b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 8 deletions.
2 changes: 1 addition & 1 deletion AZDialogView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AZDialogView"
s.version = "1.3.3"
s.version = "1.3.4"
s.summary = "A highly customizable alert dialog controller that mimics Snapchat's alert dialog."
s.homepage = "https://github.com/Minitour/AZDialogViewController"
s.license = "MIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class ViewController: UIViewController {
return true
}

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

dialogController.show(in: self)
}


var tableViewDialogController: AZDialogViewController?

func tableViewDialog(){
let dialog = AZDialogViewController(title: "Switch Account", message: nil,widthRatio: 1.0)

tableViewDialogController = dialog

dialog.showSeparator = false

let container = dialog.container
Expand All @@ -367,6 +370,7 @@ class ViewController: UIViewController {
tableView.delegate = self
tableView.dataSource = self
tableView.separatorColor = .clear
//tableView.bouncesZoom = false
tableView.bounces = false

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

dialog.gestureRecognizer.delegate = self
dialog.dismissDirection = .bottom

dialog.show(in: self) { dialog in
dialog.contentOffset = self.view.frame.height / 2.0 - dialog.estimatedHeight / 2.0 + 15
}
Expand Down Expand Up @@ -409,6 +416,9 @@ class ViewController: UIViewController {
}

}

var shouldDismiss: Bool = false
var velocity: CGFloat = 0.0

}

Expand All @@ -417,7 +427,49 @@ class ViewController: UIViewController {
extension ViewController: UITableViewDelegate{
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
dismiss(animated: true, completion: nil)
tableViewDialogController?.dismiss()
}


func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let duration = Double(1.0/abs(velocity))
if scrollView.isAtTop {
if shouldDismiss {
tableViewDialogController?.animationDuration = duration
tableViewDialogController?.dismiss()
}else {
tableViewDialogController?.applyAnimatedTranslation(-velocity * 35.0,duration: min(max(duration,0.2),0.4))
}

}
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
shouldDismiss = velocity.y < -3.0
self.velocity = velocity.y
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.bounces = !scrollView.isAtTop

}
}

extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

let optionalTableView: UITableView? = otherGestureRecognizer.view as? UITableView

guard let tableView = optionalTableView,
let panGesture = gestureRecognizer as? UIPanGestureRecognizer,
let direction = panGesture.direction
else { return false }

if tableView.isAtTop && direction == .down {
return true
} else {
return false
}
}
}

Expand Down Expand Up @@ -498,3 +550,49 @@ class HighlightableButton: UIButton{
}


public extension UIScrollView {

var isAtTop: Bool {
return contentOffset.y <= verticalOffsetForTop
}

var isAtBottom: Bool {
return contentOffset.y >= verticalOffsetForBottom
}

var verticalOffsetForTop: CGFloat {
let topInset = contentInset.top
return -topInset
}

var verticalOffsetForBottom: CGFloat {
let scrollViewHeight = bounds.height
let scrollContentSizeHeight = contentSize.height
let bottomInset = contentInset.bottom
let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight
return scrollViewBottomOffset
}

}

public enum PanDirection: Int {
case up, down, left, right
public var isVertical: Bool { return [.up, .down].contains(self) }
public var isHorizontal: Bool { return !isVertical }
}

public extension UIPanGestureRecognizer {

public var direction: PanDirection? {
let velocity = self.velocity(in: view)
let isVertical = abs(velocity.y) > abs(velocity.x)
switch (isVertical, velocity.x, velocity.y) {
case (true, _, let y) where y < 0: return .up
case (true, _, let y) where y > 0: return .down
case (false, let x, _) where x > 0: return .right
case (false, let x, _) where x < 0: return .left
default: return nil
}
}

}
42 changes: 39 additions & 3 deletions Sources/AZDialogViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ open class AZDialogViewController: UIViewController{
@objc
open fileprivate(set) lazy var blurView: UIVisualEffectView = UIVisualEffectView()

@objc fileprivate(set) lazy var gestureRecognizer = UIPanGestureRecognizer(
target: self,
action: #selector(AZDialogViewController.handlePanGesture(_:))
)

@objc
open var dialogView: UIView? {
return baseView
Expand Down Expand Up @@ -306,6 +311,7 @@ open class AZDialogViewController: UIViewController{
UIView.animate(withDuration: animationDuration){
baseView.center = CGPoint(x: center.x ,y: center.y + offset)
}
baseView.lastLocation = baseView.center
}
}
}
Expand Down Expand Up @@ -615,6 +621,11 @@ open class AZDialogViewController: UIViewController{
controller.present(self, animated: false,completion: nil)
}


/// 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.
///
/// - Parameter view: The view you wish to add.
/// - Returns: Self.
@objc
@discardableResult
open func section(view: UIView) -> Self {
Expand Down Expand Up @@ -656,6 +667,10 @@ open class AZDialogViewController: UIViewController{
return self
}


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

Expand All @@ -665,6 +680,28 @@ open class AZDialogViewController: UIViewController{
sections[index].removeFromSuperview()
}
}


/// This function applies translation to the dialog with animation and then returns it to its original position.
///
/// - Parameters:
/// - translation: The translation value.
/// - duration: The duration of the overall animation.
open func applyAnimatedTranslation(_ translation: CGFloat, duration: TimeInterval = 0.35){

let yTranslation = baseView.lastLocation.y + translation

UIView.animate(withDuration: duration / 2.0, animations: { [weak self] in
guard let `self` = self else { return }
self.baseView.center = CGPoint(x: self.baseView.lastLocation.x , y: yTranslation)
}) { (completion) in
var point = self.view.center
point.y = point.y + self.contentOffset
UIView.animate(withDuration: duration / 2.0) {
[weak self] () -> Void in self?.baseView.center = point
}
}
}

//MARK: - Overriding methods

Expand Down Expand Up @@ -778,7 +815,7 @@ open class AZDialogViewController: UIViewController{
createGesutre(for: baseView)
createGesutre(for: container)

baseView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(AZDialogViewController.handlePanGesture(_:))))
baseView.addGestureRecognizer(gestureRecognizer)

baseView.layer.cornerRadius = 15
baseView.layer.backgroundColor = alertBackgroundColor?.cgColor ?? UIColor.white.cgColor
Expand Down Expand Up @@ -878,12 +915,11 @@ open class AZDialogViewController: UIViewController{
self.widthRatio = widthRatio
}


/// Selector method - used to handle the dragging.
///
/// - Parameter sender: The Gesture Recognizer.
@objc internal func handlePanGesture(_ sender: UIPanGestureRecognizer){

//if panning is disabled return
if !allowDragGesture{
return
Expand Down

0 comments on commit 7f4262b

Please sign in to comment.