From 8d132a7b32bdc98f956ca1d740d221b3924bf074 Mon Sep 17 00:00:00 2001 From: Pei Date: Sat, 6 Jul 2019 01:01:53 +0800 Subject: [PATCH] - add auto dismiss option to action - update sample project for usage of auto dismiss option --- Library/PMAlertAction.swift | 5 ++++- Library/PMAlertController.swift | 9 ++++++++- PMAlertControllerSample/ViewController.swift | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Library/PMAlertAction.swift b/Library/PMAlertAction.swift index 9271c84..b3ebb2f 100644 --- a/Library/PMAlertAction.swift +++ b/Library/PMAlertAction.swift @@ -19,15 +19,17 @@ import UIKit fileprivate var action: (() -> Void)? open var actionStyle : PMAlertActionStyle + open var autoDismiss : Bool open var separator = UIImageView() init(){ self.actionStyle = .cancel + self.autoDismiss = true super.init(frame: CGRect.zero) } - @objc public convenience init(title: String?, style: PMAlertActionStyle, action: (() -> Void)? = nil){ + @objc public convenience init(title: String?, style: PMAlertActionStyle, autoDismiss: Bool = true, action: (() -> Void)? = nil){ self.init() self.action = action @@ -39,6 +41,7 @@ import UIKit self.actionStyle = style style == .default ? (self.setTitleColor(UIColor(red: 191.0/255.0, green: 51.0/255.0, blue: 98.0/255.0, alpha: 1.0), for: UIControl.State())) : (self.setTitleColor(UIColor.gray, for: UIControl.State())) + self.autoDismiss = autoDismiss self.addSeparator() } diff --git a/Library/PMAlertController.swift b/Library/PMAlertController.swift index c0d5661..6d9dff3 100755 --- a/Library/PMAlertController.swift +++ b/Library/PMAlertController.swift @@ -106,7 +106,14 @@ import UIKit alertActionStackView.axis = .horizontal } - alertAction.addTarget(self, action: #selector(PMAlertController.dismissAlertController(_:)), for: .touchUpInside) + if alertAction.autoDismiss { + alertAction.addTarget(self, action: #selector(PMAlertController.dismissAlertController(_:)), for: .touchUpInside) + } + } + + @objc open func dismiss(animated: Bool, style: PMAlertActionStyle) { + self.animateDismissWithGravity(style) + self.dismiss(animated: animated, completion: nil) } @objc fileprivate func dismissAlertController(_ sender: PMAlertAction){ diff --git a/PMAlertControllerSample/ViewController.swift b/PMAlertControllerSample/ViewController.swift index 8332812..5303ac5 100755 --- a/PMAlertControllerSample/ViewController.swift +++ b/PMAlertControllerSample/ViewController.swift @@ -30,9 +30,11 @@ class ViewController: UIViewController { print("Cancel") })) - alertVC.addAction(PMAlertAction(title: "Allow", style: .default, action: { () in + let allowAction = PMAlertAction(title: "Allow", style: .default, autoDismiss: false, action: { () in print("Allow") - })) + alertVC.dismiss(animated: true, style: .default) + }) + alertVC.addAction(allowAction) self.present(alertVC, animated: true, completion: nil) }