gpt4 book ai didi

ios - 如何在 swift 4 的 IBInspectable 中将 Radius 的某些边缘转角?

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:54 25 4
gpt4 key购买 nike

所以这是运行良好的代码

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

但我需要在我的代码中多次使用它所以我创建了 View 类并希望在 IBInspectable 中使用它作为可选的每条边来更改 Storyboard中的角半径所以我使用了它但它没有显示在 Storyboard中

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

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

最佳答案

只需将您的类更改为 @IBDesignable 即可在 Storyboard中查看

喜欢

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

你可以在 Storyboard中看到它

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
}

关于ios - 如何在 swift 4 的 IBInspectable 中将 Radius 的某些边缘转角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com