Skip to content

Commit bc0ea41

Browse files
committed
Add isIncludedInPinLayout property to Layoutable
1 parent ce929a6 commit bc0ea41

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Sources/Extensions/CALayer+PinLayout.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import QuartzCore
2121

2222
extension CALayer: Layoutable {
23+
private struct pinlayoutAssociatedKeys {
24+
static var pinlayoutIsIncludedInPinLayout = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
25+
}
26+
2327
public typealias PinView = CALayer
2428

2529
public var superview: CALayer? {
@@ -38,6 +42,15 @@ extension CALayer: Layoutable {
3842
return PinLayout(view: self, keepTransform: false)
3943
}
4044

45+
public var isIncludedInPinLayout: Bool {
46+
get {
47+
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayout) as? Bool ?? true
48+
}
49+
set {
50+
objc_setAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayout, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
51+
}
52+
}
53+
4154
public func getRect(keepTransform: Bool) -> CGRect {
4255
if keepTransform {
4356
/*

Sources/Extensions/UIView+PinLayout.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ import Foundation
2222
#if os(iOS) || os(tvOS)
2323
import UIKit
2424

25+
extension UIView {
26+
fileprivate struct pinlayoutAssociatedKeys {
27+
static var pinlayoutIsIncludedInPinLayout = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
28+
static var pinlayoutAutoSizingRect = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
29+
static var pinlayoutAutoSizingRectWithMargins = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
30+
}
31+
}
32+
2533
extension UIView: Layoutable, SizeCalculable {
2634
public typealias PinView = UIView
2735

@@ -37,6 +45,15 @@ extension UIView: Layoutable, SizeCalculable {
3745
return PinLayoutObjCImpl(view: self, keepTransform: true)
3846
}
3947

48+
public var isIncludedInPinLayout: Bool {
49+
get {
50+
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayout) as? Bool ?? true
51+
}
52+
set {
53+
objc_setAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayout, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
54+
}
55+
}
56+
4057
public func getRect(keepTransform: Bool) -> CGRect {
4158
guard !Pin.autoSizingInProgress || autoSizingRect == nil else { return autoSizingRect ?? CGRect.zero }
4259

@@ -94,11 +111,6 @@ extension UIView: Layoutable, SizeCalculable {
94111
}
95112

96113
extension UIView: AutoSizeCalculable {
97-
private struct pinlayoutAssociatedKeys {
98-
static var pinlayoutAutoSizingRect = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
99-
static var pinlayoutAutoSizingRectWithMargins = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
100-
}
101-
102114
private var autoSizingRect: CGRect? {
103115
get {
104116
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutAutoSizingRect) as? CGRect

Sources/Layoutable.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ public protocol Layoutable: AnyObject, Equatable, CustomDebugStringConvertible {
2929
var superview: PinView? { get }
3030
var subviews: [PinView] { get }
3131

32+
var isIncludedInPinLayout: Bool { get set }
33+
3234
func getRect(keepTransform: Bool) -> CGRect
3335
func setRect(_ rect: CGRect, keepTransform: Bool)
3436

3537
func convert(_ point: CGPoint, to view: PinView?) -> CGPoint
3638

3739
func isLTR() -> Bool
3840
}
41+
42+
extension Layoutable {
43+
public var includedSubviews: [PinView] {
44+
return subviews.filter(\.isIncludedInPinLayout)
45+
}
46+
}

0 commit comments

Comments
 (0)