【问题标题】:SWIFT - Convert String to UIKit elementSWIFT - 将字符串转换为 UIKit 元素
【发布时间】:2017-05-04 21:16:50
【问题描述】:

也许这是一个尴尬的问题,但可以将 String 转换为 UIKit 元素。我的意思是:

我有以下 UIViewController

import UIKit

class BlurVC: UIViewController {


    let bg: UIImageView = {
       let image = UIImageView()
        image.image = #imageLiteral(resourceName: "IMG_9293")
        image.contentMode = .scaleAspectFill
        image.backgroundColor = .red
        return image
    }()

    let blurView: UIVisualEffectView = {
        let view = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()



        bg.frame = view.bounds
        view.addSubview(bg)
        blurView.frame = bg.bounds
        bg.addSubview(blurView)

        setBlurButton()
    }

    func setBlurButton() {

        let blurEffects = ["extraLight", "light", "dark"]

        for index in 0..<blurEffects.count{

            let button = UIButton()
            button.setTitle(blurEffects[index], for: .normal)
            button.addTarget(self, action: #selector(setBlurEffect(_ :)), for: .touchUpInside)
            button.frame = CGRect(x: 10, y: 10  + index * 30, width: 100, height: 30)
            view.addSubview(button)
        }
    }

    func setBlurEffect(_ sender: UIButton){
        print(sender.titleLabel?.text)
        if let blurEffect = sender.titleLabel?.text {
            blurView.effect = UIBlurEffect(style: ".\(blurEffect)")
        }
    }
}

我用 3 个 UIBlurEffectStyle 参数的标题动态创建了 3 个按钮。

接下来我想根据标题标签设置 UIBlurEffectStyle 的值。

当然,xCode 警告我它不能将字符串转换为 UIBlurEffectStyle。

我可以将此字符串转换为 UIBlurEffectStyle 吗?如果是,怎么做?

谢谢。

【问题讨论】:

    标签: ios swift string uikit


    【解决方案1】:

    UIBlurEffectStyle 基于 Int,因此不会自动从 String 转换。

    但您可以手动完成。这是一个带有 init 的扩展,它将 UIBlurEffectStyle 案例作为 Strings:

    extension UIBlurEffectStyle {
    
        init?(with string: String) {
            switch string {
            case "extraLight":
                self = .extraLight
            case "light":
                self = .light
            case "dark":
                self = .dark
            case "regular":
                self = .regular
            case "prominent":
                self = .prominent
            default:
                return nil
            }
        }
    }
    

    这不是一个容易出错的初始化器。由于它是字符串类型的,因此您需要在使用它之前验证它是否有效。

    【讨论】:

    • Plus 1 但不适用于 iOS 9.x。您需要添加一些@available 检查。
    • 你是对的@OzgurVatansever,最后两种情况应该被注释掉或在 iOS 10 之前的版本中正确处理。我不想加载整个项目,所以我只是使用游乐场:)
    【解决方案2】:

    你可以添加一个扩展:

    extension UIBlurEffectStyle {
        static func styleFromString(_ string: String) -> UIBlurEffectStyle {
            switch string {
            case "extraLight":
                return .extraLight
            case "light":
                return .light
            case "dark":
                return .dark
            default:
                return .regular /* NOTE: .regular is not available in < iOS 10 */
            }
        }
    }
    

    在您的情况下使用:

    blurView.effect  = UIBlurEffect(style: UIBlurEffectStyle.styleFromString("blurEffect"))
    

    【讨论】:

    • @PEEJWEEJ 好点。谢谢。将更新答案以供任何人阅读。
    • 感谢这个问题,它也可以正常工作@janusbalatbat
    猜你喜欢
    • 2014-08-30
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2013-03-03
    • 2017-01-07
    • 2011-03-07
    • 2013-04-03
    相关资源
    最近更新 更多