|
9 | 9 | import UIKit
|
10 | 10 |
|
11 | 11 | ///Represents container view for one page content
|
12 |
| -final class SlideContainerView { |
| 12 | +final class SlideContainerView: UIView { |
13 | 13 | private var internalView: UIView
|
14 |
| - private let containerView = UIView() |
15 |
| - |
16 |
| - ///Constraints for internal view, allow to change UI positioning for container view |
17 |
| - var constraints = [NSLayoutConstraint]() |
18 | 14 |
|
| 15 | + private var oldSize: CGSize = .zero |
| 16 | + |
19 | 17 | /// - Parameter view: The view to show as content.
|
20 | 18 | init(view: UIView) {
|
21 |
| - containerView.clipsToBounds = true |
22 |
| - containerView.translatesAutoresizingMaskIntoConstraints = false |
23 |
| - containerView.addSubview(view) |
24 |
| - NSLayoutConstraint.activate([ |
25 |
| - view.topAnchor.constraint(equalTo: containerView.topAnchor), |
26 |
| - view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor), |
27 |
| - view.widthAnchor.constraint(equalTo: containerView.widthAnchor), |
28 |
| - view.heightAnchor.constraint(equalTo: containerView.heightAnchor) |
29 |
| - ]) |
30 |
| - internalView = view |
| 19 | + self.internalView = view |
| 20 | + super.init(frame: .zero) |
| 21 | + self.clipsToBounds = true |
| 22 | + self.addSubview(view) |
31 | 23 | }
|
32 | 24 |
|
33 |
| - ///Sets container view frame width or height to 0, |
34 |
| - ///internal view saves frame size to avoid unnecessary layout work |
35 |
| - func hide(direction: SlideDirection) { |
36 |
| - var removeConstraints: [NSLayoutConstraint] = [] |
37 |
| - var addConstraints: [NSLayoutConstraint] = [] |
38 |
| - |
39 |
| - let layoutAttribute: NSLayoutConstraint.Attribute |
40 |
| - let containerViewAnchor: NSLayoutDimension |
41 |
| - let internalViewAnchor: NSLayoutDimension |
42 |
| - let constraintConstant: CGFloat |
43 |
| - switch direction { |
44 |
| - case .horizontal: |
45 |
| - layoutAttribute = .width |
46 |
| - containerViewAnchor = containerView.widthAnchor |
47 |
| - internalViewAnchor = internalView.widthAnchor |
48 |
| - constraintConstant = containerView.frame.size.width |
49 |
| - case .vertical: |
50 |
| - layoutAttribute = .height |
51 |
| - containerViewAnchor = containerView.heightAnchor |
52 |
| - internalViewAnchor = internalView.heightAnchor |
53 |
| - constraintConstant = containerView.frame.size.height |
54 |
| - } |
55 |
| - |
56 |
| - guard let constraintIndex = constraints.index(where: { $0.firstAttribute == layoutAttribute }), |
57 |
| - let viewConstraintIndex = containerView.constraints.index(where: { $0.firstAttribute == layoutAttribute }) else { |
58 |
| - return |
59 |
| - } |
60 |
| - removeConstraints.append(constraints[constraintIndex]) |
61 |
| - constraints.remove(at: constraintIndex) |
62 |
| - let constraint = containerViewAnchor.constraint(equalToConstant: 0) |
63 |
| - addConstraints.append(constraint) |
64 |
| - constraints.append(constraint) |
65 |
| - |
66 |
| - removeConstraints.append(containerView.constraints[viewConstraintIndex]) |
67 |
| - let viewConstraint = internalViewAnchor.constraint(equalToConstant: constraintConstant) |
68 |
| - addConstraints.append(viewConstraint) |
69 |
| - NSLayoutConstraint.deactivate(removeConstraints) |
70 |
| - NSLayoutConstraint.activate(addConstraints) |
| 25 | + required init?(coder aDecoder: NSCoder) { |
| 26 | + fatalError("init(coder:) has not been implemented") |
71 | 27 | }
|
72 | 28 |
|
73 |
| - ///Returns container view frame width or height to match superview frame |
74 |
| - func show(direction: SlideDirection) { |
75 |
| - guard let superView = containerView.superview else { |
| 29 | + override func layoutSubviews() { |
| 30 | + guard self.oldSize != self.bounds.size else { |
76 | 31 | return
|
77 | 32 | }
|
78 |
| - var removeConstraints: [NSLayoutConstraint] = [] |
79 |
| - var addConstraints: [NSLayoutConstraint] = [] |
80 |
| - |
81 |
| - let layoutAttribute: NSLayoutConstraint.Attribute |
82 |
| - let containerViewAnchor: NSLayoutDimension |
83 |
| - let internalViewAnchor: NSLayoutDimension |
84 |
| - let superviewAnchor: NSLayoutDimension |
85 |
| - switch direction { |
86 |
| - case .horizontal: |
87 |
| - layoutAttribute = .width |
88 |
| - containerViewAnchor = containerView.widthAnchor |
89 |
| - internalViewAnchor = internalView.widthAnchor |
90 |
| - superviewAnchor = superView.widthAnchor |
91 |
| - case .vertical: |
92 |
| - layoutAttribute = .height |
93 |
| - containerViewAnchor = containerView.heightAnchor |
94 |
| - internalViewAnchor = internalView.heightAnchor |
95 |
| - superviewAnchor = superView.heightAnchor |
96 |
| - } |
97 | 33 |
|
98 |
| - guard let constraintIndex = constraints.index(where: { $0.firstAttribute == layoutAttribute }), |
99 |
| - let viewConstraintIndex = internalView.constraints.index(where: { $0.firstAttribute == layoutAttribute }) else { |
| 34 | + super.layoutSubviews() |
| 35 | + |
| 36 | + guard !self.isHidden else { |
100 | 37 | return
|
101 | 38 | }
|
102 |
| - removeConstraints.append(constraints[constraintIndex]) |
103 |
| - constraints.remove(at: constraintIndex) |
104 |
| - let constraint = containerViewAnchor.constraint(equalTo: superviewAnchor) |
105 |
| - addConstraints.append(constraint) |
106 |
| - constraints.append(constraint) |
107 | 39 |
|
108 |
| - removeConstraints.append(internalView.constraints[viewConstraintIndex]) |
109 |
| - let viewConstraint = internalViewAnchor.constraint(equalTo: containerViewAnchor) |
110 |
| - addConstraints.append(viewConstraint) |
111 |
| - NSLayoutConstraint.deactivate(removeConstraints) |
112 |
| - NSLayoutConstraint.activate(addConstraints) |
113 |
| - } |
114 |
| -} |
115 |
| - |
116 |
| -///Viewable protocol implementation |
117 |
| -private typealias ViewableImplementation = SlideContainerView |
118 |
| -extension ViewableImplementation: Viewable { |
119 |
| - var view: UIView { |
120 |
| - return containerView |
| 40 | + self.internalView.frame = self.bounds |
| 41 | + self.oldSize = self.bounds.size |
121 | 42 | }
|
122 | 43 | }
|
0 commit comments