You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

  1. //
  2. // PMAlertAction.swift
  3. // PMAlertController
  4. //
  5. // Created by Paolo Musolino on 07/05/16.
  6. // Copyright © 2018 Codeido. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public enum PMAlertActionStyle : Int {
  10. case `default`
  11. case cancel
  12. }
  13. @objc open class PMAlertAction: UIButton {
  14. fileprivate var action: (() -> Void)?
  15. open var actionStyle : PMAlertActionStyle
  16. open var separator = UIImageView()
  17. init(){
  18. self.actionStyle = .cancel
  19. super.init(frame: CGRect.zero)
  20. }
  21. @objc public convenience init(title: String?, style: PMAlertActionStyle, action: (() -> Void)? = nil){
  22. self.init()
  23. self.action = action
  24. self.addTarget(self, action: #selector(PMAlertAction.tapped(_:)), for: .touchUpInside)
  25. self.setTitle(title, for: UIControl.State())
  26. self.titleLabel?.font = UIFont(name: "Avenir-Heavy", size: 17)
  27. self.actionStyle = style
  28. 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()))
  29. self.addSeparator()
  30. }
  31. required public init?(coder aDecoder: NSCoder) {
  32. fatalError("init(coder:) has not been implemented")
  33. }
  34. @objc func tapped(_ sender: PMAlertAction) {
  35. //Action need to be fired after alert dismiss
  36. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
  37. self?.action?()
  38. }
  39. }
  40. @objc fileprivate func addSeparator(){
  41. separator.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
  42. self.addSubview(separator)
  43. // Autolayout separator
  44. separator.translatesAutoresizingMaskIntoConstraints = false
  45. separator.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
  46. separator.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor, constant: 8).isActive = true
  47. separator.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor, constant: -8).isActive = true
  48. separator.heightAnchor.constraint(equalToConstant: 1).isActive = true
  49. }
  50. }