swift - 使用@IBInspectable 时何时使用 didset 或 get set

标签 swift

看了不同的教程之后。我不知道什么时候使用 didset 或 get set 来更新变量。 谁能解释一下何时使用 didset 或 get set 的更多细节?

 @IBInspectable var circleColor: UIColor = UIColor.redColor() {
    didSet { //after properties are set in storyboard, update here
        circleLayer.strokeColor = circleColor.CGColor
        self.toggleButon()
    }
}
/**
    Radius of RadioButton circle.
*/
@IBInspectable var circleRadius: CGFloat = 5.0
@IBInspectable var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
}

对于圆半径,它不必使用 didset 来更新它的值。我听不懂。

最佳答案

这里我给大家举个例子,试着给大家讲解一下如何使用,希望对大家有所帮助。

我在这里为 UIView 使用这个类来设置和设置 Story Board 我的类名在这里“MyCustomView”

import Foundation
import UIKit
import QuartzCore

/// Computed properties, based on the backing CALayer property, that are visible in Interface Builder.
@IBDesignable public class MyCustomView: UIView {
    /// When positive, the background of the layer will be drawn with rounded corners. Also effects the mask generated by the `masksToBounds' property. Defaults to zero. Animatable.
    @IBInspectable var cornerRadius: Double {
        get {
            return Double(self.layer.cornerRadius)
        }
        set {
            self.layer.cornerRadius = CGFloat(newValue)
        }
    }
    
    /// The width of the layer's border, inset from the layer bounds. The border is composited above the layer's content and sublayers and includes the effects of the `cornerRadius' property. Defaults to zero. Animatable.
    @IBInspectable var borderWidth: Double {
        get {
            return Double(self.layer.borderWidth)
        }
        set {
            self.layer.borderWidth = CGFloat(newValue)
        }
    }
    
    /// The color of the layer's border. Defaults to opaque black. Colors created from tiled patterns are supported. Animatable.
    @IBInspectable var borderColor: UIColor? {
        get {
            return UIColor(CGColor: self.layer.borderColor!)
        }
        set {
            self.layer.borderColor = newValue?.CGColor
        }
    }
    
    /// The color of the shadow. Defaults to opaque black. Colors created from patterns are currently NOT supported. Animatable.
    @IBInspectable var shadowColor: UIColor? {
        get {
            return UIColor(CGColor: self.layer.shadowColor!)
        }
        set {
            self.layer.shadowColor = newValue?.CGColor
        }
    }
    
    /// The opacity of the shadow. Defaults to 0. Specifying a value outside the [0,1] range will give undefined results. Animatable.
    @IBInspectable var shadowOpacity: Float {
        get {
            return self.layer.shadowOpacity
        }
        set {
            self.layer.shadowOpacity = newValue
        }
    }
    
    /// The shadow offset. Defaults to (0, -3). Animatable.
    @IBInspectable var shadowOffset: CGSize {
        get {
            return self.layer.shadowOffset
        }
        set {
            self.layer.shadowOffset = newValue
        }
    }
    
    /// The blur radius used to create the shadow. Defaults to 3. Animatable.
    @IBInspectable var shadowRadius: Double {
        get {
            return Double(self.layer.shadowRadius)
        }
        set {
            self.layer.shadowRadius = CGFloat(newValue)
        }
    }
}

你可以将它与 Storyboard一起使用,用你的“UIView”导入这个类

enter image description here

之后你会看到一些 而你直接在这里设置view cornet radius, shadow and Shadow

enter image description here

无需运行代码即可直接在 Storyboard中看到结果

从这段代码输出到这里

enter image description here

关于swift - 使用@IBInspectable 时何时使用 didset 或 get set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37739482/

相关文章:

ios - 具有 queryEqual 值的 observeSingleEvent 不起作用

ios - 无法在 Xcode 项目上使用 Apollo 编译从 ruby​​ gem 导出的 GraphQL 查询

ios - 将 sails.io 与 iOS swift 应用程序连接

swift - 更改 SKAudioNodes 音量

ios - 在swift中将值输出到typedef指针

swift - 我应该如何发出 swift 超时的警报

macos - 无法在 Swift OS X 中接收 NSWorkspaceDidWakeNotification

ios - Swift 泛型函数接受枚举

ios - 如何使用 Swift 获取 youtube channel 的所有播放列表?

ios - 如何使用 XCUITest 测试 UIImageView 是否包含具有特定名称的图像?