Ios 创建可与IBInspectable一起使用的OptionSet

Ios 创建可与IBInspectable一起使用的OptionSet,ios,swift,swift3,ibinspectable,optionsettype,Ios,Swift,Swift3,Ibinspectable,Optionsettype,我正在尝试创建一个可与@IBInspectable结合使用的optionstart,这似乎在Swift 2.2中是可能的 我遇到了一个库,它似乎使用了一个optionstart和@IBInspectable组合使用(IBInspectable是第一个集合,结构实际上是在类的底部创建的) 我认为这是可能的,因为BooleanType似乎从swift2.3 我像这样编写了我的OptionSet,但它不能与@IBInspectable结合使用,因为它在BooleanType所在的地方不受支持(我想这就

我正在尝试创建一个可与
@IBInspectable
结合使用的
optionstart
,这似乎在
Swift 2.2中是可能的

我遇到了一个库,它似乎使用了一个
optionstart
@IBInspectable
组合使用(
IBInspectable
是第一个集合,
结构实际上是在类的底部创建的)

我认为这是可能的,因为
BooleanType
似乎从
swift2.3

我像这样编写了我的
OptionSet
,但它不能与
@IBInspectable
结合使用,因为它在
BooleanType
所在的地方不受支持(我想这就是它在前面提到的库的代码中工作的原因)


有人知道如何确保它在Swift 3中工作吗?所以我确实找到了一种方法,通过编写某种适配器来使用它

我非常确信这可以做得更好,如果有人有办法做到这一点,请毫不犹豫地提供您的解决方案,但我现在就是这样做的

public struct Corners: OptionSet {
private enum Corner: Int, CustomStringConvertible {
    case TopLeft=1
    case TopRight=2
    case BottomLeft=4
    case BottomRight=8
    case All=16

    public var description: String {
        var shift = 0
        while (rawValue.hashValue >> shift != 1) { shift += 1 }
        return ["topleft", "topright", "bottomleft", "bottomright", "all"][shift]
    }
}
public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ shape: Corner) { self.rawValue = shape.rawValue }

static let TopLeft = Corners(Corner.TopLeft)
static let TopRight = Corners(Corner.TopRight)
static let BottomLeft = Corners(Corner.BottomLeft)
static let BottomRight = Corners(Corner.BottomRight)
static let All = [TopLeft, TopRight, BottomLeft, BottomRight]
}

// Needed to split the string that's provided in the @IBInspectable. and remove any possible spaces the user introduced
extension String {
    func getStrings() -> [String] {
        var stringArray: [String] = []
        let strings = self.characters.split{$0 == ","}.map(String.init)
        for s in strings {
            let string = s.removeSpaces()
            stringArray.append(string)
        }
        return stringArray
    }

    func removeSpaces() -> String {
        if self.characters.first == " " {
            var copy = self
            copy.characters.removeFirst()
            return copy.removeSpaces()
        } else {
            return self
        }
    }
}
然后我的
@IBInspectable
看起来像这样

var corners = [Corners.TopLeft]
@IBInspectable public var onCorners: String = "" {
    willSet {
        corners = []
        for s in newValue.lowercased().getStrings() {
            switch s {
                case "topleft":
                    corners.append(Corners.TopLeft)
                case "topright":
                    corners.append(Corners.TopRight)
                case "bottomleft":
                    corners.append(Corners.BottomLeft)
                case "bottomright":
                    corners.append(Corners.BottomRight)
                case "all":
                    corners = Corners.All
                default:
                    return
            }
        }
    }
    didSet {
        // Do your logic here
    }
}
我是怎么做到的

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