创建可与IBInspectable一起使用的OptionSet

时间:2017-03-21 20:38:56

标签: ios swift swift3 ibinspectable optionsettype

我正在尝试创建OptionSet,可以与@IBInspectable结合使用。Swift 2.2

似乎可以这样做

我遇到了this库,似乎正在使用OptionSet@IBInspectable结合使用(IBInspectable是第一组和{{1}实际上是在类的底部创建的)

我认为这是可能的,因为structure

之后似乎删除了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) }

中有效

2 个答案:

答案 0 :(得分:1)

所以我找到了一种方法,可以通过编写某种适配器来使用它。

我很确定它可以做得更好,如果有人有办法这样做,请不要犹豫,提供你的解决方案,但这就是我现在做的方式

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

答案 1 :(得分:0)

这是我的表现

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