Skip to content

Instantly share code, notes, and snippets.

@b3ll
Last active February 1, 2021 12:50
Show Gist options
  • Select an option

  • Save b3ll/b317d463fa3f9ebaee5d41c6a8dcfc32 to your computer and use it in GitHub Desktop.

Select an option

Save b3ll/b317d463fa3f9ebaee5d41c6a8dcfc32 to your computer and use it in GitHub Desktop.

Revisions

  1. Adam Bell revised this gist Dec 22, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LayoutInvalidating.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    propertyWrapper
    @propertyWrapper
    public final class LayoutInvalidating<Value> where Value: Equatable {

    public static subscript<EnclosingSelf>(
  2. Adam Bell revised this gist Dec 22, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion LayoutInvalidating.swift
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,6 @@ public final class LayoutInvalidating<Value> where Value: Equatable {
    observed[keyPath: storageKeyPath].stored = newValue

    if newValue != oldValue {
    // TODO: call wrapper instance with enclosing self
    observed.setNeedsLayout()
    }
    }
  3. Adam Bell created this gist Dec 22, 2020.
    46 changes: 46 additions & 0 deletions LayoutInvalidating.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    propertyWrapper
    public final class LayoutInvalidating<Value> where Value: Equatable {

    public static subscript<EnclosingSelf>(
    _enclosingInstance observed: EnclosingSelf,
    wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
    storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, LayoutInvalidating>
    ) -> Value where EnclosingSelf: UIView {
    get {
    return observed[keyPath: storageKeyPath].stored
    }
    set {
    let oldValue = observed[keyPath: storageKeyPath].stored
    observed[keyPath: storageKeyPath].stored = newValue

    if newValue != oldValue {
    // TODO: call wrapper instance with enclosing self
    observed.setNeedsLayout()
    }
    }
    }

    public var wrappedValue: Value {
    get { fatalError("called wrappedValue getter") }
    set { fatalError("called wrappedValue setter") }
    }

    public init(wrappedValue: Value) {
    self.stored = wrappedValue
    }

    // MARK: - Private

    private var stored: Value
    }

    // Test for Playgrounds

    public class SomeView: UIView {

    @LayoutInvalidating public var volume: CGFloat = 0.0

    }

    let a = SomeView(frame: .zero)
    a.volume = 20.0