@@ -111,7 +111,7 @@ open class AZDialogViewController: UIViewController{
111111 open override func dismiss( animated: Bool = true , completion: ( ( ) -> Void ) ? = nil ) {
112112 if animated {
113113 UIView . animate ( withDuration: 0.2 , animations: { ( ) -> Void in
114- self . baseView. center. y = ( self . baseView . superview ? . frame . maxY) ! + ( self . baseView. frame . midY)
114+ self . baseView. center. y = self . view . bounds . maxY + ( self . baseView. bounds . midY)
115115 self . view. backgroundColor = . clear
116116 } , completion: { ( complete) -> Void in
117117 super. dismiss ( animated: false , completion: completion)
@@ -348,7 +348,7 @@ open class AZDialogViewController: UIViewController{
348348 super. viewDidAppear ( animated)
349349 if !didInitAnimation{
350350 didInitAnimation = true
351- baseView. center. y = ( baseView . superview ? . frame . maxY) ! + ( baseView. frame . midY)
351+ baseView. center. y = self . view . bounds . maxY + baseView. bounds . midY
352352 baseView. isHidden = false
353353 UIView . animate ( withDuration: 0.2 , delay: 0 , usingSpringWithDamping: 1 , initialSpringVelocity: 6.0 , options: [ ] , animations: { ( ) -> Void in
354354 self . baseView. center = ( self . baseView. superview? . center) !
@@ -396,6 +396,7 @@ open class AZDialogViewController: UIViewController{
396396 fontName: String = " AvenirNext-Medium " ,
397397 boldFontName: String = " AvenirNext-DemiBold " ) {
398398 self . init ( nibName: nil , bundle: nil )
399+
399400 mTitle = title
400401 mMessage = message
401402 self . spacing = spacing
@@ -418,55 +419,68 @@ open class AZDialogViewController: UIViewController{
418419 let translation = sender. translation ( in: self . view)
419420 baseView. center = CGPoint ( x: baseView. lastLocation. x , y: baseView. lastLocation. y + translation. y)
420421
421- if sender. state == UIGestureRecognizerState . ended{
422+ let returnToCenter : ( CGPoint , Bool ) -> Void = { ( finalPoint, animate) in
423+ if !animate {
424+ self . baseView. center = finalPoint
425+ return
426+ }
427+ UIView . animate ( withDuration: 0.35 , delay: 0 , usingSpringWithDamping: 0.5 , initialSpringVelocity: 2.0 , options: [ ] , animations: { ( ) -> Void in
428+ self . baseView. center = finalPoint
429+ } , completion: { ( complete) -> Void in
430+ } )
431+ }
432+
433+ let dismissInDirection : ( CGPoint ) -> Void = { ( finalPoint) in
434+ UIView . animate ( withDuration: 0.2 , animations: { ( ) -> Void in
435+ self . baseView. center = finalPoint
436+ self . view. backgroundColor = . clear
437+ } , completion: { ( complete) -> Void in
438+ self . dismiss ( animated: false , completion: nil )
439+ } )
440+ }
441+
442+ var finalPoint = ( baseView. superview? . center) !
443+
444+ if sender. state == . ended{
422445
423446 let velocity = sender. velocity ( in: view)
424447 let mag = sqrtf ( Float ( velocity. x * velocity. x) + Float( velocity. y * velocity. y) )
425448 let slideMult = mag / 200
426- var finalPoint = ( baseView. superview? . center) !
427449 let dismissWithGesture = dismissDirection != . none ? true : false
428450
429- let returnToCenter = {
430- UIView . animate ( withDuration: 0.35 , delay: 0 , usingSpringWithDamping: 0.5 , initialSpringVelocity: 2.0 , options: [ ] , animations: { ( ) -> Void in
431- self . baseView. center = finalPoint
432- } , completion: { ( complete) -> Void in
433- } )
434- }
435451
436- let dismissInDirection = {
437- UIView . animate ( withDuration: 0.2 , animations: { ( ) -> Void in
438- self . baseView. center = finalPoint
439- self . view. backgroundColor = . clear
440- } , completion: { ( complete) -> Void in
441- self . dismiss ( animated: false , completion: nil )
442- } )
443- }
444452
445453 if dismissWithGesture && slideMult > 1 {
446454 //dismiss
447455 if velocity. y > 0 {
448456 //dismiss downward
449457 if dismissDirection == . bottom || dismissDirection == . both {
450458 finalPoint. y = ( baseView. superview? . frame. maxY) ! + ( baseView. bounds. midY)
451- dismissInDirection ( )
459+ dismissInDirection ( finalPoint )
452460 } else {
453- returnToCenter ( )
461+ returnToCenter ( finalPoint , true )
454462 }
455463 } else {
456464
457465 //dismiss upward
458466 if dismissDirection == . top || dismissDirection == . both {
459467 finalPoint. y = - ( baseView. bounds. midY)
460- dismissInDirection ( )
468+ dismissInDirection ( finalPoint )
461469 } else {
462- returnToCenter ( )
470+ returnToCenter ( finalPoint , true )
463471 }
464472 }
465473 } else {
466474 //return to center
467- returnToCenter ( )
475+ returnToCenter ( finalPoint , true )
468476 }
469477 }
478+
479+ if sender. state == . cancelled || sender. state == . failed{
480+ returnToCenter ( finalPoint, false )
481+ }
482+
483+
470484 }
471485
472486 /// Selector method - used to handle view touch.
0 commit comments