1

我正在尝试创建一个OptionSet可以@IBInspectableSwift 2.2

我遇到了这个库,它似乎OptionSet正在结合使用@IBInspectable(这IBInspectable是第一个集合,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)
}

有谁知道如何确保它可以在Swift 3

4

2 回答 2

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
    }
}
于 2017-03-22T20:32:05.513 回答
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
}
于 2018-07-24T09:02:57.720 回答