【问题标题】:swift IOS convert a function to custom classswift IOS将函数转换为自定义类
【发布时间】:2016-06-09 07:45:15
【问题描述】:

谁能给我点一下我的大脑?我很难从 Swift 中理解“类”。 每次我创建一个类(自定义)我都会失败。所以我通常会做什么。我创建了一个函数并一遍又一遍地使用它。只是感觉不对。

例如。 我想创建一个类,所以只需访问它,我就可以创建 UILabel 而不使用下面的方法

func addView(title:String, fontColor:UIColor, fontType:UIFont, bgColor:UIColor, x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat) {

    let myView = UILabel()
    myView.text = title
    myView.sizeToFit()
    myView.numberOfLines = 0
    myView.textColor = fontColor
    myView.font = fontType
    myView.backgroundColor = bgColor
    myView.frame = CGRectMake(x, y, width, height)
    self.view.addSubview(myView)
}

我只是不知道如何理解创建类。 谁能给我一个很好的例子?谢谢

【问题讨论】:

    标签: swift function class


    【解决方案1】:

    Swift 中的类是灵活构造的构建块。与常量、变量和函数类似,用户可以定义类属性和方法。

    在这种情况下,您不需要创建Class。有关我的声明的更多信息,您可以查看here: 这可能会浪费资源,很少有 UILabel 自定义不能证明子类化是合理的。 您可以创建一个更有用的文件,例如命名为 Utils.swift,其中包含您的方法集合:

    import UIKit
    
    func addView(title:String, fontColor:UIColor, fontType:UIFont, bgColor:UIColor, x:CGFloat, y:CGFloat, width:CGFloat, height:CGFloat) -> UILabel {
        let myView = UILabel()
        myView.text = title
        myView.sizeToFit()
        myView.numberOfLines = 0
        myView.textColor = fontColor
        myView.font = fontType
        myView.backgroundColor = bgColor
        myView.frame = CGRectMake(x, y, width, height)
        return myView
    }
    

    在 Swift 中,您不需要将文件导入其他类,因此在您的类中您可以这样做:

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let myLabel = addView("hello world",fontColor:UIColor.redColor(),fontType:UIFont(name: "HelveticaNeue-UltraLight", size: 30),bgColor:UIColor.redColor(),x:0,y:0,width:200,height:25)
            self.view.addsubview(myLabel)
        }
    }
    

    否则,如果您需要对UILabel 进行子类化以进行相关变化:

        import UIKit
    
        class MyCustomLabel: UILabel {
    
            override init(frame: CGRect) {
                super.init(frame: frame)
                sharedInit()
            }
    
            init(title: String, fontColor: UIColor, fontType: UIFont, bgColor: UIColor, x: CGFloat, y: CGFloat, width:CGFloat, height: CGFloat){
                let frame = CGRectMake(x, y, width, height)
                super.init(frame: frame)
                self.text = title
                self.sizeToFit()
                self.numberOfLines = 0
                self.textColor = textColor
                self.font = fontType
                self.backgroundColor = bgColor
                sharedInit()
            }
    
            required init(coder aDecoder: NSCoder) {
                super.init(coder: aDecoder)
                sharedInit()
            }
    
            func sharedInit() {
                userInteractionEnabled = true
                // add others common init features..
            }
        }
    

    【讨论】:

    • 感谢您的回复。了解你的功能部分,但你能解释一下你创建的那个类吗?我不明白覆盖 init(frame:CGRecT) 被覆盖的部分意味着它是强制性的吗?
    • 当您编写与超类指定初始化程序匹配的子类初始化程序时,您实际上是提供了对该指定初始化程序的覆盖。因此,您必须在子类的初始化器定义之前编写 override 修饰符。您可以在此处找到更多信息developer.apple.com/library/ios/documentation/Swift/Conceptual/…
    【解决方案2】:

    创建 UILabel 的子类。然后使用您的预期设置声明您的自定义初始化程序。

    import UIKit
    
        class MyLabel: UILabel {
            override init(frame: CGRect) {
                super.init(frame: frame)
            }
            // custom initializer
            init(title: String, fontColor: UIColor, fontType: UIFont, bgColor: UIColor, x: CGFloat, y: CGFloat, width:CGFloat, height: CGFloat){
                let frame = CGRectMake(x, y, width, height)
                super.init(frame: frame)
                self.text = title
                self.sizeToFit()
                self.numberOfLines = 0
                self.textColor = textColor
                self.font = fontType
                self.backgroundColor = bgColor
            }
    
            func initialSetup() {
                self.sizeToFit()
                self.numberOfLines = 0
            }
    
            required init?(coder aDecoder: NSCoder) {
                fatalError("init(coder:) has not been implemented")
            }
        }
    

    现在创建自定义类的对象并添加为主视图的子视图。

    let myView = MyLabel(title: "demo title", fontColor: UIColor.whiteColor(), fontType: UIFont.systemFontOfSize(12), bgColor: UIColor.grayColor(), x: 0, y: 0, width: 300, height: 300)
    self.view.addSubview(myView)
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多