This is the sixth day of my participation in Gwen Challenge

Toast is the prompt and load component commonly used in normal development. Swift Toast code is not very large, but applies most of the functions of the Swift radix syntax; For consolidating the previous learning struct, class, closure, func, variable definition, extension grammar is very helpful, in the actual practice application, to understand the grammar has a deeper level; At the same time, it is not very difficult for beginners and will not feel very difficult. For old hands, you can ignore it, but you can learn his component design ideas. The overall design is simple and universal, with high cohesion and low coupling and extensible. He not only supports his own TOAST presentation, but also supports user customization of toasts, as well as location customization. In addition to the usual up, middle, and down can also specify point.. , etc.

In just 783 lines of code, more than 90% of toASTfunctions are completed in daily use. Basically achieve high cohesion low coupling expansibility; Is a UI component library that is well worth learning to emulate

Toast’s overall construction idea:

  1. Create UIView classes so that all inherited UIView classes have toast display capabilities

    public extension UIView {
    Copy the code
  2. Create a ToastView with displayable

    // Create & Display
    func makeToast(_ message: String? .duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, title: String? = nil.image: UIImage? = nil.style: ToastStyle = ToastManager.shared.style, completion: ((_ didTap: Bool) - >Void)? = nil) {
            do {
              // toastViewForMessage Actually creates the View
                let toast = try toastViewForMessage(message, title: title, image: image, style: style)
              // display method
                showToast(toast, duration: duration, position: position, completion: completion)
            } catch ToastError.missingParameters {
                print("Error: message, title, and image are all nil")}catch{}}Copy the code
  3. Universal showToast; We not only create our own Toast, but also support user-defined View display

    func showToast(_ toast: UIView.duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, completion: ((_ didTap: Bool) - >Void)? = nil) {
            let point = position.centerPoint(forToast: toast, inSuperview: self)
            showToast(toast, duration: duration, point: point, completion: completion)
        }
    Copy the code
  4. Support for creating modal ToastActivityViews

    func makeToastActivity(_ position: ToastPosition) {
            // sanity
            guard objc_getAssociatedObject(self.&ToastKeys.activityView) as? UIView = = nil else { return }
            // Create a generic load indicator view
            let toast = createToastActivityView()
            let point = position.centerPoint(forToast: toast, inSuperview: self)
            makeToastActivity(toast, point: point)
        }
    Copy the code

Main logic sorting

  1. Create UIView categories, this line will support all view types and toast prompt; That’s a cleaner code

    public extension UIView {
    Copy the code
    self.view.makeToast("This is a toast")
    Copy the code
    self.view.makeToast("This is a custom: specify animation time and position of Toast", duration: 3.0, position: .top)
    
    Copy the code
  2. Create default toast

  3. Show the toast

    1. Display default toast

      1. self.view.makeToast("This is a toast")
        Copy the code
    2. Display of user-defined toasts is also supported

      1. self.view.showToast(myView)
        Copy the code
  4. Support for creating modal Loading indicators

    self.view.makeToastActivity(.center)
    Copy the code
  5. Customize the toast style by defining ToastStyle

    1. public struct ToastStyle {
      
          public init(a) {}
        	// Default background color
          public var backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.8)
          // Title color
          public var titleColor: UIColor = T.color
      		// Content color
          public var messageColor: UIColor = T.color
          /// Maximum width percentage
          public var maxWidthPercentage: CGFloat = 0.8 {
              didSet {
                  maxWidthPercentage = max(min(maxWidthPercentage, 1.0), 0.0)}}public var maxHeightPercentage: CGFloat = 0.8 {
              didSet {
                  maxHeightPercentage = max(min(maxHeightPercentage, 1.0), 0.0)}}public var horizontalPadding: CGFloat = 10.0
          public var verticalPadding: CGFloat = 10.0
        
        	.More}Copy the code
    2. Create a new Style
      var style = ToastStyle(a)// Change the color of messageLabel
      style.messageColor = .blue
      
      // Display a toast by specifying the style
      self.view.makeToast("This is a toast", duration: 3.0, position: .bottom, style: style)
      
      // You can also style the global toast
      ToastManager.shared.style = style
      Copy the code
  6. Customize toast display positions by defining ToastPosition: up, middle, and down (default)

    public enum ToastPosition {
        case top
        case center
        case bottom
    }
    Copy the code
  7. ToastManager configures the default Toast style, position, animation time, and whether clicking to disappear the Toast is supported

    public class ToastManager {
        
        / / the singleton
        public static let shared = ToastManager(a)// Default style
        public var style = ToastStyle(a)// Whether to click disappear to hide toast is supported. Default is true
        public var isTapToDismissEnabled = true
        
        // Whether to display in order of click or immediately
        public var isQueueEnabled = false
        
        // Show time
        public var duration: TimeInterval = 3.0
        
        // Display position: the default is bottom
        public var position: ToastPosition = .bottom
        
    }
    Copy the code