如何在 swift 4 的 IBInspectable 中对 Radius 一些边进行转角?

how to corner Radius some edges in IBInspectable in swift 4?

所以这是运行良好的代码

let path = UIBezierPath(roundedRect:viewToRound.bounds,
                    byRoundingCorners:[.topRight, .bottomLeft],
                    cornerRadii: CGSize(width: 20, height:  20))

let maskLayer = CAShapeLayer()

maskLayer.path = path.cgPath
viewToRound.layer.mask = maskLayer

但我需要在我的代码中多次使用它所以我创建了 class 用于查看并希望在 IBInspectable 中使用它作为可选的每条边来更改故事板中的角半径所以我使用这个但是它没有显示在故事板中

@IBInspectable
open var cornerEdges: CGSize {
    get {
        let path = UIBezierPath(roundedRect:self.bounds,
                                byRoundingCorners:[.topRight, .bottomLeft],
                                cornerRadii: CGSize(width: 20, height:  20))

        let maskLayer = CAShapeLayer()

        maskLayer.path = path.cgPath
        self.layer.mask = maskLayer
        return maskLayer.path as! CGSize
    }
    set(value) {
        maskLayer.path = value
    }
}

那么我应该如何在我的代码中执行此操作?

只需将您的 class 更改为 @IBDesignable 即可在故事板中查看

喜欢

@IBDesignable // Add this to your class
class PlusButton: AnyUIClass { }

你可以在故事板中看到它

如何从 storybaord 获取角和半径

这是一个示例代码

@IBInspectable
open var cornerEdges: CGSize = CGSize(width: 20, height: 20)
@IBInspectable  var topLeft: Bool = true
@IBInspectable  var topRight: Bool = true
@IBInspectable  var bottomLeft: Bool = true
@IBInspectable  var bottomRight: Bool = true

override func awakeFromNib() {

    var options = UIRectCorner()
    if topLeft {
       options =  options.union(.topLeft)
    }
    if topRight {
       options =  options.union(.topRight)
    }
    if bottomLeft {
      options =  options.union(.bottomLeft)
    }
    if bottomRight {
      options =  options.union(.bottomRight)
    }


    let path = UIBezierPath(roundedRect:self.bounds,
                            byRoundingCorners:options,
                            cornerRadii: self.cornerEdges)

    let maskLayer = CAShapeLayer()

    maskLayer.path = path.cgPath
    self.layer.mask = maskLayer
}

所以如果你想改变边角的高度和宽度你可以使用@PrashantTukadiya answer 但是如果你只想为角设置一个数字你可以使用这个代码

 @IBInspectable
open var cornerEdges : CGFloat = 0
@IBInspectable  var topLeft: Bool = false
@IBInspectable  var topRight: Bool = false
@IBInspectable  var bottomLeft: Bool = false
@IBInspectable  var bottomRight: Bool = false
 override func awakeFromNib() {
    super.awakeFromNib()

    var options = UIRectCorner()
    if topLeft {
        options =  options.union(.topLeft)
    }
    if topRight {
        options =  options.union(.topRight)
    }
    if bottomLeft {
        options =  options.union(.bottomLeft)
    }
    if bottomRight {
        options =  options.union(.bottomRight)
    }


    let path = UIBezierPath(roundedRect:self.bounds,
                            byRoundingCorners:options,
                            cornerRadii: CGSize(width: self.cornerEdges, height: self.cornerEdges))

    let maskLayer = CAShapeLayer()

    maskLayer.path = path.cgPath
    self.layer.mask = maskLayer
}