diff --git a/README.md b/README.md index 2bffc0d..094ec53 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 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..