From 2893eef9e47ecb9a96d8fb567c2fb7ff687668ba Mon Sep 17 00:00:00 2001 From: Martin Tjandra Date: Thu, 19 May 2016 15:54:44 +0700 Subject: [PATCH 1/3] - Add function to receive multiple buttons Title and buttons Color in array. - Allow some buttons color entry to be nil, and it will automatically use previous button color. - Update swift syntax to version 2.2. --- SweetAlert/SweetAlert.swift | 106 +++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/SweetAlert/SweetAlert.swift b/SweetAlert/SweetAlert.swift index 52c18c8..cb94274 100644 --- a/SweetAlert/SweetAlert.swift +++ b/SweetAlert/SweetAlert.swift @@ -34,6 +34,7 @@ public class SweetAlert: UIViewController { var imageView:UIImageView? var subTitleTextView = UITextView() var userAction:((isOtherButton: Bool) -> Void)? = nil + var userActionMore:((selectedButton: Int) -> Void)? = nil let kFont = "Helvetica" init() { @@ -122,21 +123,22 @@ public class SweetAlert: UIViewController { } var totalWidth: CGFloat = 0.0 - if buttons.count == 2 { - totalWidth = buttonRect[0].size.width + buttonRect[1].size.width + kWidthMargin + 40.0 - } - else{ - totalWidth = buttonRect[0].size.width + 20.0 + for btnRect in buttonRect { + totalWidth += btnRect.size.width; } + totalWidth += 20.0 * CGFloat(buttons.count); + totalWidth += kWidthMargin * CGFloat(buttons.count - 1); + print (totalWidth); + y += kHeightMargin var buttonX = (kContentWidth - totalWidth ) / 2.0 - for var i = 0; i < buttons.count; i++ { + for i in 0..= 3) { + if userActionMore != nil { + SweetAlertContext.shouldNotAnimate = true + userActionMore!(selectedButton: buttonIndex) + SweetAlertContext.shouldNotAnimate = false + } } - + UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in self.view.alpha = 0.0 }) { (Bool) -> Void in @@ -284,7 +294,7 @@ public class SweetAlert: UIViewController { let button: UIButton = UIButton(type: UIButtonType.Custom) button.setTitle(otherButtonTitle, forState: UIControlState.Normal) button.backgroundColor = otherButtonColor - button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside) + button.addTarget(self, action: #selector(SweetAlert.pressed(_:)), forControlEvents: UIControlEvents.TouchUpInside) button.tag = 1 buttons.append(button) } @@ -300,6 +310,74 @@ public class SweetAlert: UIViewController { animateAlert() } } + + public func showAlert(title: String, + subTitle: String?, + style: AlertStyle, + buttonTitles: [String], + buttonColors: [UIColor?], + action: ((selectedButton: Int) -> Void)? = nil) { + userActionMore = action + let window: UIWindow = UIApplication.sharedApplication().keyWindow! + window.addSubview(view) + window.bringSubviewToFront(view) + view.frame = window.bounds + self.setupContentView() + self.setupTitleLabel() + self.setupSubtitleTextView() + + switch style { + case .Success: + self.animatedView = SuccessAnimatedView() + + case .Error: + self.animatedView = CancelAnimatedView() + + case .Warning: + self.animatedView = InfoAnimatedView() + + case let .CustomImag(imageFile): + if let image = UIImage(named: imageFile) { + self.imageView = UIImageView(image: image) + } + case .None: + self.animatedView = nil + } + + self.titleLabel.text = title + if subTitle != nil { + self.subTitleTextView.text = subTitle + } + buttons = [] + var lastColor = UIColor.colorFromRGB(0xAEDEF4); + for i in 0.. Date: Thu, 19 May 2016 16:00:56 +0700 Subject: [PATCH 2/3] Add even more buttons example --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2bffc0d..231e202 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ SweetAlert().showAlert("Here's a message!", subTitle: "It's pretty, isn't it?", ```swift SweetAlert().showAlert("Good job!", subTitle: "You clicked the button!", style: AlertStyle.Success) ``` -#####Warning message and Chained Animated Success messge on completion: +#####Warning message and Chained Animated Success message on completion: ```swift SweetAlert().showAlert("Are you sure?", subTitle: "You file will permanently delete!", style: AlertStyle.Warning, buttonTitle:"Cancel", buttonColor:UIColorFromRGB(0xD0D0D0) , otherButtonTitle: "Yes, delete it!", otherButtonColor: UIColorFromRGB(0xDD6B55)) { (isOtherButton) -> Void in if isOtherButton == true { @@ -32,6 +32,14 @@ SweetAlert().showAlert("Are you sure?", subTitle: "You file will permanently del } } ``` +#####Now with even more buttons: +```swift + SweetAlert().showAlert("Are you sure?", subTitle: "You files will permanently delete!", style: AlertStyle.Warning, buttonTitles: ["OK", "All", "Cancel", "Exit"], buttonColors: [UIColor.greenColor(), nil, UIColor.grayColor(), UIColor.redColor()]) { (selectedButton) -> Void in + print ("Selected button index: \(selectedButton)") + // starting from index zero + } +``` + #####Chained Alerts on actions with custom button colors: ```swift From 747cebb05184d72401bbd4d735cb7f24e09b398a Mon Sep 17 00:00:00 2001 From: Martin Tjandra Date: Thu, 19 May 2016 16:14:52 +0700 Subject: [PATCH 3/3] Fix spacing on readme.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 231e202..094ec53 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,10 @@ SweetAlert().showAlert("Are you sure?", subTitle: "You file will permanently del ``` #####Now with even more buttons: ```swift - SweetAlert().showAlert("Are you sure?", subTitle: "You files will permanently delete!", style: AlertStyle.Warning, buttonTitles: ["OK", "All", "Cancel", "Exit"], buttonColors: [UIColor.greenColor(), nil, UIColor.grayColor(), UIColor.redColor()]) { (selectedButton) -> Void in - print ("Selected button index: \(selectedButton)") - // starting from index zero - } +SweetAlert().showAlert("Are you sure?", subTitle: "You files will permanently delete!", style: AlertStyle.Warning, buttonTitles: ["OK", "All", "Cancel", "Exit"], buttonColors: [UIColor.greenColor(), nil, UIColor.grayColor(), UIColor.redColor()]) { (selectedButton) -> Void in + print ("Selected button index: \(selectedButton)") + // starting from index zero +} ```