如何在Swift 4的IBInspectable中将Radius的某些边角化?

时间:2018-07-24 05:09:19

标签: ios swift cornerradius ibinspectable

这是运行良好的代码

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

但是我需要在代码中多次使用它,所以我创建了用于查看的类,并希望在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
    }
}

那我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

只需将您的班级更改为@IBDesignable即可在情节提要中查看

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

您可以在情节提要中看到它

enter image description here

如何从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
}

答案 1 :(得分:1)

因此,如果您想同时更改高度和宽度边缘角,可以使用@PrashantTukadiya答案。但是,如果您只想为角设置一个数字,则可以使用此代码

 @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
}