Swift开发学习

自己在Swift项目中使用或者看到的好用的框架

2022-10-14  本文已影响0人  我是一个大胖墩
今年年初跳槽,顺便也从 OC 转为了 Swift ,所以之前 OC 版的框架就暂时不更新了,重新写了这篇文章,是我在 Swift 开发过程中用到、或者看到,个人觉得比较好用的框架,有新的偶遇,也会更新的,有兴趣的可以去瞄一眼
private func customCocoaDebug() {
    //--- If want to custom CocoaDebug settings ---
//        CocoaDebug.serverURL = "google.com"
//        CocoaDebug.ignoredURLs = ["aaa.com", "bbb.com"]
//        CocoaDebug.onlyURLs = ["ccc.com", "ddd.com"]
//        CocoaDebug.ignoredPrefixLogs = ["aaa", "bbb"]
//        CocoaDebug.onlyPrefixLogs = ["ccc", "ddd"]
//        CocoaDebug.emailToRecipients = ["aaa@gmail.com", "bbb@gmail.com"]
//        CocoaDebug.emailCcRecipients = ["ccc@gmail.com", "ddd@gmail.com"]
    CocoaDebug.mainColor = "#fd9727"
//        CocoaDebug.additionalViewController = TestController.init()
    
    //Deprecated! If want to support protobuf, check branch: origin/protobuf_support
    //--- If use Google's Protocol buffers ---
//        CocoaDebug.protobufTransferMap = [
//            "your_api_keywords_1": ["your_protobuf_className_1"],
//            "your_api_keywords_2": ["your_protobuf_className_2"],
//            "your_api_keywords_3": ["your_protobuf_className_3"]
//        ]
    
    //--- If want to manual enable App logs (Take effect the next time when app starts) ---
    CocoaDebugSettings.shared.enableLogMonitoring = true
}

记得在didFinishLaunchingWithOptions 内调用

重写一下打印方法,这个不要写在Appdelegate类里面,平级写

//MARK: - override Swift `print` method
public func MYLog<T>(file: String = #file, function: String = #function, line: Int = #line, _ message: T, color: UIColor = .white) {
#if DEBUG
    Swift.print(message)
    _SwiftLogHelper.shared.handleLog(file: file, function: function, line: line, message: message, color: color)
#endif
}

而在需要观察打印log的地方这样写:

MYLog("测试cocoaDebug有没有用", color: .red)
private func setupLaunchAd() {
      XHLaunchAd.setLaunch(.launchScreen)

      let imageAdConfiguration = XHLaunchImageAdConfiguration()
      imageAdConfiguration.frame = CGRectMake(0, 0, UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
      imageAdConfiguration.imageNameOrURLString = "转场动画.gif"
      imageAdConfiguration.gifImageCycleOnce = false
      imageAdConfiguration.imageOption = .refreshCached
      imageAdConfiguration.contentMode = .scaleAspectFill
      imageAdConfiguration.openModel = "https://www.xxxx.com"
      imageAdConfiguration.showFinishAnimate = .fadein
      imageAdConfiguration.showFinishAnimateTime = 0.8
      imageAdConfiguration.skipButtonType = .roundProgressText
      imageAdConfiguration.showEnterForeground = true

      XHLaunchAd.imageAd(with: imageAdConfiguration, delegate: self)
}
class DeviceToolModel: HandyJSON {
    
    var toolTitle: String = ""
    var toolSubtitle: [String] = [String]()
    
    required init() {
        
    }
    
    init(toolTitle: String, toolSubtitle: [String]) {
        self.toolTitle = toolTitle
        self.toolSubtitle = toolSubtitle
    }
    
}

这是用法:

setupSVGColors(svgName: "c0301", configs: self.setupSVGModel(colors: ["#FF0000", "#00FA9A", "#FF0000", "#FF0000"]), view: self.imageView)
setupSVGColors(svgName: "c0302", configs: self.setupSVGModel(colors: ["#FF0000", "#00FA9A", "#FF0000", "#FF0000"]), view: imageView1)
setupSVGColors(svgName: "c0303", configs: self.setupSVGModel(colors: ["#FF0000", "#00FA9A", "#FF0000", "#FF0000"]), view: imageView2)
setupSVGColors(svgName: "c0304", configs: self.setupSVGModel(colors: ["#FF0000", "#00FA9A", "#FF0000", "#FF0000"]), view: imageView3)
setupSVGColors(svgName: "c0305", configs: self.setupSVGModel(colors: ["#FF0000", "#00FA9A", "#FF0000", "#FF0000"]), view: imageView4)

这是当时为 SVG 改颜色写的一个简单的 model

class svgTintColorModel {
    var layerId: String = ""
    var color: String = ""
}

因为我们的 SVG 加了不同透明度,所以层级会变得很多,遍历的层级也要同步

private func setupSVGModel(colors: [String]) -> [svgTintColorModel] {
        var models = [svgTintColorModel]()
        colors.enumerated().forEach { (index, color) in
            let model = svgTintColorModel()
            model.layerId = "0\(colors.count)0\(index + 1)00"
            model.color = color
            models.append(model)
        }
        return models
    }

private func setupSVGColors(svgName: String, configs: [svgTintColorModel], view: UIImageView) {
        let svgImage = SVGKImage.init(named: svgName)
        configs.enumerated().forEach { index, config in
            svgImage?.caLayerTree.sublayers?.enumerated().forEach({ layer in
                autoreleasepool {
                    log.debug("第一层name是:\(layer.element.name ?? "")")
                    if (layer.element.name?.contains(config.layerId)) == true {
                        let shapeLayer = layer.element as! CAShapeLayer
                        shapeLayer.fillColor = UIColor.init(hex: config.color).cgColor
                        log.debug("第一层找到一个")
                    }
                    
                    layer.element.sublayers?.enumerated().forEach({ secondLayer in
                        log.debug("第二层name是:\(secondLayer.element.name ?? "")")
                        if (secondLayer.element.name?.contains(config.layerId)) == true {
                            let shapeLayer = secondLayer.element as! CAShapeLayer
                            shapeLayer.fillColor = UIColor.init(hex: config.color).cgColor
                            log.debug("第二层找到一个")
                        }
                        
                        secondLayer.element.sublayers?.enumerated().forEach({ thirdLayer in
                            log.debug("第三层name是:\(thirdLayer.element.name ?? "")")
                            if (thirdLayer.element.name?.contains(config.layerId)) == true {
                                let shapeLayer = thirdLayer.element as! CAShapeLayer
                                shapeLayer.fillColor = UIColor.init(hex: config.color).cgColor
                                log.debug("第三层找到一个")
                            }

                            thirdLayer.element.sublayers?.enumerated().forEach({ fourthLayer in
                                log.debug("第四层name是:\(fourthLayer.element.name ?? "")")
                                if (fourthLayer.element.name?.contains(config.layerId)) == true {
                                    let shapeLayer = fourthLayer.element as! CAShapeLayer
                                    shapeLayer.fillColor = UIColor.init(hex: config.color).cgColor
                                    log.debug("第四层找到一个")
                                }
                            })
                        })
                    })
                }
            })
        }
        let svgImageView = SVGKLayeredImageView.init(svgkImage: svgImage)
        view.image = svgImageView?.image.uiImage
    }
private func popupCustomView() {
      var attributes = EKAttributes.centerFloat
      attributes.entryBackground = .color(color: EKColor(UIColor(white: 1, alpha: 1)))
      attributes.screenBackground = .color(color: EKColor(UIColor(white: 1, alpha: 1)))
      //遮罩层触摸是忽略、转发到窗口还是退出
      attributes.screenInteraction = .absorbTouches
      attributes.entryInteraction = .forward
      attributes.entranceAnimation = .translation
      attributes.exitAnimation = .translation
      attributes.displayDuration = .infinity
      let view = UIView()
      view.addSubview(self.poupView)
      self.poupView.snp.makeConstraints { make in
          make.center.equalToSuperview()
          make.width.equalTo(UIScreen.main.bounds.width)
          make.height.equalTo(UIScreen.main.bounds.height)
      }
      self.poupView.startAnimation()
      SwiftEntryKit.display(entry: self.poupView, using: attributes)
}
pod 'FLEX', :configurations => ['Debug']
FLEXManager.shared.showExplorer()
pod 'CocoaDebug', :configurations => ['Debug']
extension AppDelegate {
    
    private func customCocoaDebug() {
        //--- If want to custom CocoaDebug settings ---
//        CocoaDebug.serverURL = "google.com"
//        CocoaDebug.ignoredURLs = ["aaa.com", "bbb.com"]
//        CocoaDebug.onlyURLs = ["ccc.com", "ddd.com"]
//        CocoaDebug.ignoredPrefixLogs = ["aaa", "bbb"]
//        CocoaDebug.onlyPrefixLogs = ["ccc", "ddd"]
//        CocoaDebug.emailToRecipients = ["aaa@gmail.com", "bbb@gmail.com"]
//        CocoaDebug.emailCcRecipients = ["ccc@gmail.com", "ddd@gmail.com"]
        CocoaDebug.mainColor = "#fd9727"
//        CocoaDebug.additionalViewController = TestController.init()
        
        //Deprecated! If want to support protobuf, check branch: origin/protobuf_support
        //--- If use Google's Protocol buffers ---
//        CocoaDebug.protobufTransferMap = [
//            "your_api_keywords_1": ["your_protobuf_className_1"],
//            "your_api_keywords_2": ["your_protobuf_className_2"],
//            "your_api_keywords_3": ["your_protobuf_className_3"]
//        ]
        
        //--- If want to manual enable App logs (Take effect the next time when app starts) ---
        CocoaDebugSettings.shared.enableLogMonitoring = true
    }
    
}
pod 'LLDebugToolSwift' , :configurations => ['Debug']
extension AppDelegate {
    
    func setupLLDebug() {
        LLDebugTool.shared().startWorking{ config in
            config.logStyle = .detail
        }
    }
    
}
pod 'LifetimeTracker'
class SkeletonViewDemoController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(self.label)
        self.label.snp.makeConstraints { make in
            make.leading.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(100)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(self.imageView)
        self.imageView.snp.makeConstraints { make in
            make.top.equalTo(self.label.snp.bottom).offset(20)
            make.width.height.equalTo(100)
            make.centerX.equalToSuperview()
        }
        
        self.label.showAnimatedGradientSkeleton()
        self.imageView.showAnimatedGradientSkeleton()
        
        Timer.after(5) {
            self.label.hideSkeleton()
            self.imageView.hideSkeleton()
        }
    }
    
    //MARK: - lazy load -
    private lazy var label: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 20)
        label.textColor = .red
        label.isSkeletonable = true
        label.text = "1f1wfagwagwag12512t3t1g1g"
        return label
    }()
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.isSkeletonable = true
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(named: "ic_art")
        return imageView
    }()
}

效果:


SkeletonView效果.gif
pod 'WCDB.cpp'
var config = SwiftMessages.Config()
config.presentationStyle = .center
config.presentationContext = .window(windowLevel: .alert)
config.prefersStatusBarHidden = true
config.duration = .forever
config.dimMode = .gray(interactive: true)
config.interactiveHide = false
SwiftMessages.show(config: config, view: self.popupView)

效果:


SwiftMessagesSegue.gif
上一篇下一篇

猜你喜欢

热点阅读