IOS a bullet frame package

Time:2022-1-5

1. Background

Recently, when studying the design mode, the company just added a bullet frame to meet the needs of the company. Although the previously encapsulated ones can be met, the coupling degree is too high and there are too many changes, so it sprouted to write a bullet frame that conforms to the design mode and has better encapsulation and expansibility than before.

2. Ideas

  • The pop-up box is divided into three parts: title, content and interactive button
  • Using abstract factory mode to generate two types of products, text displayitem(title, content and text all belong to this) and for interactionitem(button)
  • Then use the constructor mode (although it is the builder mode, because the construction mode is single, the commander and abstract construction class are omitted, and there are only specific product classes and specific builder classes, so it is called the constructor mode) to assemble each product part to form a bullet frame class
  • Various custom settings are also flexibly provided to the caller through chain calls (the constructor mode is also used here)

3. Advantages

  • Highly encapsulated, external calls only know the factory class and builder class, regardless of how to implement and integrate inside the bullet box
  • Highly developed, if you are not satisfied with the existingitem, you can add Abstract products and specific product classes based on the factory pattern for expansion, with low coupling and low embeddedness.
  • The animation effect is flexible, and you can choose the displayed and hidden animation by yourself.

4. Partial source code

Please refer to the specific source codeGitHub

LYAlertFactoryProtocol

//Abstract factory
public protocol LYAlertFactoryProtocol {
      func creatAlertShowItem(_ type:LYAlertTitleItemType, _ text:String) -> LYAlertTitleShowProtocol
     func creatAlertInteractiveItem(_ text: String)->LYAlertInteractiveItemProtocol
}
//Abstract product base class
public protocol LYAlertItemProtocol{
     func titleColor(_ color:UIColor) -> Self
     func fontSize(_ font:CGFloat) -> Self
     func attribute(_ attribute:[NSAttributedString.Key :Any])->Self
}
//Abstract text display products
public protocol LYAlertTitleShowProtocol:LYAlertItemProtocol {
      func line(_ color:UIColor,_ width:CGFloat) -> Self
     func textAlignment(_ alignment:NSTextAlignment) -> Self
    
}
//Abstract interactive products
public protocol LYAlertInteractiveItemProtocol:LYAlertItemProtocol  {
      func btnBackgroundColor(_ color:UIColor) -> Self
      func itemBackgroundColor(_ color:UIColor) -> Self
      func btnRadius(_ radius:CGFloat) -> Self
      func btnBorderWidth(_ borderWidth:CGFloat) -> Self
      func btnBorderColor(_ color:UIColor) -> Self
      func line(_ color:UIColor,_ width:CGFloat,_ type:LYLineType) -> Self
      func btnHeight(_ height:CGFloat)->Self
      func btnMarin(_ marin:CGFloat)->Self

     func action(clo:@escaping LYCommonSet.btnClosure)  -> Self
}

LYAlertFactory

//Specific factory category
public class  LYAlertFactory:LYAlertFactoryProtocol{
    public init() {}

    //Create presentation item
   public func creatAlertShowItem(_ type: LYAlertTitleItemType, _ text: String) -> LYAlertTitleShowProtocol {
     return LYAlertTitleItem.init(type, text: text)
    }
    //Create interactive item
    public func creatAlertInteractiveItem(_ text: String) -> LYAlertInteractiveItemProtocol {
        return LYAlertInteractiveItem.init(text)
   
    }
}

LYAlertBuilder

public class LYAlertBuilder {
    var alertView:LYAlertShowView?
    
   public init(_ title:LYAlertTitleShowProtocol?,_ content:LYAlertTitleShowProtocol,_ buttons:[LYAlertInteractiveItemProtocol]) {
        alertView = LYAlertShowView.init(title, content, buttons)
    }
    
   public func borderColor(_ color:UIColor) -> Self {
        self.alertView?.alertView.layer.borderColor = color.cgColor
        return self
    }
    public func borderWidth(_ width:CGFloat) -> Self {
        self.alertView?.alertView.layer.borderWidth = width
        return self
    }
    public func cornerRadius(_ radius:CGFloat) -> Self {
        self.alertView?.alertView.layer.cornerRadius = radius
        return self
    }
    public func shadowPath(_ path:CGPath) -> Self {
        self.alertView?.alertView.layer.shadowPath = path
        return self
    }
    public  func shadowOffset(_ size:CGSize) -> Self {
        self.alertView?.alertView.layer.shadowOffset = size
        return self
    }
    public func shadowRadius(_ radius:CGFloat) -> Self {
        self.alertView?.alertView.layer.shadowRadius = radius
        return self
    }
    public func shadowOpacity(_ opacity:Float) -> Self {
        self.alertView?.alertView.layer.shadowOpacity = opacity
        return self
    }
    public func shadowColor(_ color:UIColor) -> Self {
        self.alertView?.alertView.layer.shadowColor = color.cgColor
        return self
    }
    public func maskAlpha(_ alpha:CGFloat) -> Self {
        self.alertView?.blackView.backgroundColor = LYCommonSet.RGBA(r: 0, g: 0, b: 0, a: alpha)
        return self
    }
    public func showAnimation(_ type:LYAlertAnimationPopType,_ during:CFTimeInterval)->Self{
        self.alertView?.showType = type
        self.alertView?.showDuring = during
        return self
    }
    public func hideAnimation(_ type:LYAlertAnimationDissType,_ during:CFTimeInterval)->Self{
        self.alertView?.hideType = type
        self.alertView?.hideDuring = during
        return self
    }
    
    public func build()-> LYAlertShowView?{
       return alertView
    }
    
}

4. Installation

Method 1: use cocoapods

pod 'LYAlertView'

Mode 2: manual import

  • takeLYAlertViewDrag all source code in the folder into the project
  • Direct call

5. Call example

let item1 = LYAlertFactory()
                . createalertshowitem (. Title, "warm tips")
                .fontSize(17)
                .titleColor(UIColor.darkGray)
            let item2 = LYAlertFactory()
                . createalertshowitem (. Content, "are you sure you want to send a one-to-one wrong question counseling request to the teacher?")
                .fontSize(16)
                .titleColor(UIColor.darkGray)
                .line(UIColor.lightGray, 0.5)
            let item3 = LYAlertFactory()
                . createalertinteractivitem ("let me think again")
                .fontSize(15)
                .btnHeight(30)
                .btnMarin(10)
                .titleColor(UIColor.gray)
                .line(UIColor.lightGray, 0.5, .left)
                .action {
                     Print ("I'll think about it again")
                }
            let item4 = LYAlertFactory()
                . createalertinteractivitem ("confirm initiation")
                .fontSize(15)
                .btnHeight(30)
                .btnMarin(10)
                .titleColor(UIColor.brown)
                .line(UIColor.lightGray, 0.5, .left)
                .action {
                    Print ("confirm initiation")
                }
 let alert = LYAlertBuilder.init(item1, item2, [item3,item4])
                .cornerRadius(15)
                .maskAlpha(0.6)
                .showAnimation(.shakeFromTop, 0.5)
                .hideAnimation(.shakeToTop, 0.5)
                .build()
  alert?.show()

6. Effect display

1

2

3

4

Recommended Today

Redis Client On Error: Error: write ECONNABORTED Config right

Solve the redis client on error: error: write econnaborted config rightwe Problem Description: Solution: 1. First, check whether the firewall of Linux is turned on Turn off the firewall [[email protected]]# systemctl stop firewalld.service Open 6379 port number [[email protected]]# sudo firewall-cmd –zone=public –add-port=6379/tcp –permanent success [[email protected]]# sudo firewall-cmd –reload success 2. Check whether the redis startup […]