创建可与 IBInspectable 一起使用的 OptionSet

Create a OptionSet that is usable with IBInspectable

我正在尝试创建一个可以与 @IBInspectable 结合使用的 OptionSet 似乎这在 Swift 2.2

中是可行的

我遇到了 this 库,它似乎将 OptionSet@IBInspectable 结合使用(IBInspectable 是第一个集合, structure实际上是在class)

的底部创建的

我认为这是可能的,因为 BooleanType 似乎在 Swift 2.3

之后被删除了

我的 OptionSet 是这样写的,但它不能与 @IBInspectable 结合使用,因为 BooleanType 不支持它(我认为这就是它起作用的原因在前面提到的库的代码中)

public struct Shapes: OptionSet {
    private enum Shape: Int, CustomStringConvertible {
        case Circle=1, Square=2

        public var description: String {
            var shift = 0
            while (rawValue >> shift != 1) { shift += 1 }
            return ["Circle", "Square"][shift]
        }
    }
    public let rawValue: Int
    public init(rawValue: Int) { self.rawValue = rawValue }
    private init(_ shape: Shape) { self.rawValue = shape.rawValue }

    static let Circle = Shapes(Shape.Circle)
    static let Square = Shapes(Shape.Square)
}

有谁知道如何确保它能在 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{[=10=] == ","}.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
}