如何创建多个缓存的 UIColor [英] How to create several cached UIColor

查看:24
本文介绍了如何创建多个缓存的 UIColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有自定义颜色.我多次使用它们,我希望它们只分配一次.

I have custom colors within my code. I use them several times and I would like to have them allocated only once.

如果我们查看 UIColor 标题,我们可以看到以下内容:

If we get a look at UIColor headers we can see the following :

[...]

// Some convenience methods to create colors.  These colors will be as calibrated as possible.
// These colors are cached.
  
open class var black: UIColor { get } // 0.0 white

open class var darkGray: UIColor { get } // 0.333 white

[...]

我已经创建了 UIColor 的 extension,如下所示:

I've created an extension of UIColor, like so :

import UIKit

extension UIColor {
    
    class func colorWithHexString(_ hex: String) -> UIColor {
        
        print("\(#function): \(hex)")
        
        // some code, then it return a UIColor

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )

    }
    
    // Option A
    open class var myColorOne : UIColor {
        get {
            return colorWithHexString("AABBCC")
        }
    }
    
    // Option B
    class func myColorTwo() -> UIColor {
        return colorWithHexString("DDEEFF")
    }
}

从那里我可以轻松地使用我的颜色,无论是变量还是函数.

From there I can use my colors easily, with either having a variable or a function.

// A
UIColor.myColorOne

// B
UIColor.myColorTwo()

遗憾的是,我对此并不完全满意.确实,每次我想使用这些颜色时:都会进行新的 UIColor 分配.

Sadly, I'm not fully happy with that. Indeed, every time I want to use those colors : a new UIColor allocation is made.

Apple 设法使他们的颜色明显缓存.我自己也想这样做.我尝试了几件事,但似乎都不是理想的.

Apple managed to make their color cached apparently. I would like to do so myself too. I've tried several things but none seems to be ideal.

如 Swift 页面所示:免费功能 dispatch_once 在 Swift 中不再可用.

As visible on Swift page : the free function dispatch_once is no longer available in Swift.

我收到以下错误:扩展可能不包含存储的属性

它确实有效(每种颜色只创建一次)与以下内容

It does work (each color are created only once) with the following

import UIKit

class Colors : UIColor {
    
    // Singleton
    static let sharedInstance = Colors()
    
    let myColorOne : UIColor = {
        return UIColor.colorWithHexString("AABBCC")
    }()
    
    let myColorTwo : UIColor = {
        return UIColor.colorWithHexString("DDEEFF")
    }()
    
}

但它迫使我再有一个文件并像这样调用我的颜色

But it forces me to have one more file and call my colors like so

Colors.sharedInstance.myColorOne

有没有办法像 UIColor.myColorOne 那样获得我想要的颜色并像 Apple 那样缓存它们?

Isn't there any way to get the colors I want like that UIColor.myColorOne and have them cached like Apple does ?

推荐答案

您可以使用与在 Swift 中使用 dispatch_once 单例模型,即静态常量存储属性延迟初始化(并且仅初始化一次).这些可以定义直接在 UIColor 扩展中:

You can use the same approach as in Using a dispatch_once singleton model in Swift, i.e. static constant stored properties which are initialized lazily (and only once). These can be defined directly in the UIColor extension:

extension UIColor {
    convenience init(hex: String) {
        // ...          
    }

    static let myColorOne = UIColor(hex:"AABBCC")
    static let myColorTwo = UIColor(hex:"DDEEFF")
}

这篇关于如何创建多个缓存的 UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆