Skip to content

Commit 5dbddb6

Browse files
committed
Add several other actions
1 parent 52df620 commit 5dbddb6

File tree

4 files changed

+129
-17
lines changed

4 files changed

+129
-17
lines changed

ScrollStackControllerDemo/Base.lproj/Main.storyboard

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,117 @@
2121
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
2222
</view>
2323
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="a7Q-tz-rI1">
24-
<rect key="frame" x="20" y="64" width="153" height="30"/>
24+
<rect key="frame" x="20" y="64" width="100" height="30"/>
2525
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2626
<constraints>
27-
<constraint firstAttribute="width" constant="153" id="eZt-64-3CB"/>
27+
<constraint firstAttribute="width" constant="100" id="eZt-64-3CB"/>
2828
<constraint firstAttribute="height" constant="30" id="uxI-ig-f5J"/>
2929
</constraints>
30-
<state key="normal" title="Add Random Row">
30+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
31+
<state key="normal" title="Add Row">
3132
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
3233
</state>
3334
<connections>
3435
<action selector="addNewRow" destination="BYZ-38-t0r" eventType="touchUpInside" id="O6D-J0-3dz"/>
3536
</connections>
3637
</button>
38+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ljg-R8-0k8">
39+
<rect key="frame" x="128" y="64" width="100" height="30"/>
40+
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
41+
<constraints>
42+
<constraint firstAttribute="width" constant="100" id="huO-0H-rxs"/>
43+
<constraint firstAttribute="height" constant="30" id="tRQ-JP-7ZS"/>
44+
</constraints>
45+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
46+
<state key="normal" title="Hide/Show Row">
47+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
48+
</state>
49+
<connections>
50+
<action selector="hideOrShowRandomRow" destination="BYZ-38-t0r" eventType="touchUpInside" id="RIt-EZ-Kgx"/>
51+
</connections>
52+
</button>
53+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="a8U-7G-txi">
54+
<rect key="frame" x="234" y="64" width="100" height="30"/>
55+
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
56+
<constraints>
57+
<constraint firstAttribute="width" constant="100" id="HwD-9V-Np7"/>
58+
<constraint firstAttribute="height" constant="30" id="z3X-Ls-g0O"/>
59+
</constraints>
60+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
61+
<state key="normal" title="Toggle Axis">
62+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
63+
</state>
64+
<connections>
65+
<action selector="toggleAxis" destination="BYZ-38-t0r" eventType="touchUpInside" id="lTz-Zf-aGb"/>
66+
</connections>
67+
</button>
68+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1E4-jE-O3H">
69+
<rect key="frame" x="20" y="102" width="100" height="30"/>
70+
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
71+
<constraints>
72+
<constraint firstAttribute="width" constant="120" id="rPh-35-aIt"/>
73+
<constraint firstAttribute="height" constant="30" id="stZ-Fb-Grb"/>
74+
</constraints>
75+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
76+
<state key="normal" title="Remove Row">
77+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
78+
</state>
79+
<connections>
80+
<action selector="removeRow" destination="BYZ-38-t0r" eventType="touchUpInside" id="Ew3-2d-JtQ"/>
81+
</connections>
82+
</button>
83+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fOH-Lh-1KN">
84+
<rect key="frame" x="128" y="102" width="100" height="30"/>
85+
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
86+
<constraints>
87+
<constraint firstAttribute="width" constant="120" id="cCH-ma-tnV"/>
88+
<constraint firstAttribute="height" constant="30" id="cEJ-WA-VDZ"/>
89+
</constraints>
90+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
91+
<state key="normal" title="Move Row">
92+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
93+
</state>
94+
<connections>
95+
<action selector="moveRowToRandom" destination="BYZ-38-t0r" eventType="touchUpInside" id="Idy-re-ZnJ"/>
96+
</connections>
97+
</button>
98+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RgW-dD-Pae">
99+
<rect key="frame" x="236" y="102" width="100" height="30"/>
100+
<color key="backgroundColor" red="0.1333333333" green="0.31372549020000001" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
101+
<constraints>
102+
<constraint firstAttribute="width" constant="100" id="OkB-g1-BxM"/>
103+
<constraint firstAttribute="height" constant="30" id="Yvg-In-cn5"/>
104+
</constraints>
105+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
106+
<state key="normal" title="Scroll Random">
107+
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
108+
</state>
109+
<connections>
110+
<action selector="scrollToRandom" destination="BYZ-38-t0r" eventType="touchUpInside" id="MCK-YZ-Rfh"/>
111+
</connections>
112+
</button>
37113
</subviews>
38114
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
39115
<constraints>
40116
<constraint firstItem="O4f-A9-7JM" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="100" id="0Rg-6l-Mie"/>
117+
<constraint firstItem="1E4-jE-O3H" firstAttribute="top" secondItem="a7Q-tz-rI1" secondAttribute="bottom" constant="8" id="3Oj-iN-93f"/>
118+
<constraint firstItem="1E4-jE-O3H" firstAttribute="leading" secondItem="a7Q-tz-rI1" secondAttribute="leading" id="3Vd-Dh-zJV"/>
119+
<constraint firstItem="a8U-7G-txi" firstAttribute="leading" secondItem="Ljg-R8-0k8" secondAttribute="trailing" constant="6" id="3nd-oJ-x2R"/>
120+
<constraint firstItem="a8U-7G-txi" firstAttribute="centerY" secondItem="Ljg-R8-0k8" secondAttribute="centerY" id="9XS-mQ-jl2"/>
121+
<constraint firstItem="Ljg-R8-0k8" firstAttribute="leading" secondItem="a7Q-tz-rI1" secondAttribute="trailing" constant="8" id="9oc-yh-5NS"/>
41122
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="O4f-A9-7JM" secondAttribute="trailing" constant="20" id="AZR-eI-SfX"/>
42123
<constraint firstItem="a7Q-tz-rI1" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="20" id="QI2-1l-xUr"/>
124+
<constraint firstItem="Ljg-R8-0k8" firstAttribute="centerY" secondItem="a7Q-tz-rI1" secondAttribute="centerY" id="aAx-iH-qTe"/>
125+
<constraint firstItem="fOH-Lh-1KN" firstAttribute="leading" secondItem="Ljg-R8-0k8" secondAttribute="leading" id="agR-VU-Bya"/>
126+
<constraint firstItem="Ljg-R8-0k8" firstAttribute="height" secondItem="a7Q-tz-rI1" secondAttribute="height" id="cf3-CR-loY"/>
127+
<constraint firstItem="fOH-Lh-1KN" firstAttribute="trailing" secondItem="Ljg-R8-0k8" secondAttribute="trailing" id="ew0-ea-gOG"/>
43128
<constraint firstItem="a7Q-tz-rI1" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="hNF-QF-Cti"/>
44129
<constraint firstItem="O4f-A9-7JM" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="jiX-gL-rHy"/>
130+
<constraint firstItem="fOH-Lh-1KN" firstAttribute="centerY" secondItem="1E4-jE-O3H" secondAttribute="centerY" id="ma9-he-lTe"/>
131+
<constraint firstItem="1E4-jE-O3H" firstAttribute="trailing" secondItem="a7Q-tz-rI1" secondAttribute="trailing" id="ndi-rV-8Yu"/>
45132
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="O4f-A9-7JM" secondAttribute="bottom" constant="20" id="pEb-ou-B47"/>
133+
<constraint firstItem="RgW-dD-Pae" firstAttribute="centerY" secondItem="fOH-Lh-1KN" secondAttribute="centerY" id="y1e-Cv-LRW"/>
134+
<constraint firstItem="RgW-dD-Pae" firstAttribute="leading" secondItem="fOH-Lh-1KN" secondAttribute="trailing" constant="8" id="ysb-Fy-Val"/>
46135
</constraints>
47136
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
48137
</view>

ScrollStackControllerDemo/ViewController.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,37 @@ class ViewController: UIViewController {
3636
tagsVC = TagsVC.create(delegate: self)
3737
stackView.addRow(controller: tagsVC, at: .top, animated: false)
3838
}
39-
40-
@IBAction public func toggleAxis() {
41-
42-
// (stackView.rows[0].controller as! VC1).bestSize = 50
43-
// (stackView.rows[1].controller as! VC1).bestSize = 30
44-
// stackController.stackView.reloadRows(indexes: [0,1], animated: true)
45-
46-
47-
// stackController.stackView.setRowHidden(index: 0, isHidden: true, animated: true)
48-
// stackController.stackView.replaceRowAtIndex(1, withRow: otherVC, animated: true)
49-
// stackController.stackView.moveRowAtIndex(1, to: 2, animated: true)
50-
//stackController.stackView.axis = (stackController.stackView.axis == .horizontal ? .vertical : .horizontal)
51-
}
5239

5340
@IBAction public func addNewRow() {
5441
let galleryVC = GalleryVC.create()
5542
stackView.addRow(controller: galleryVC, at: .bottom, animated: true)
5643
}
44+
45+
@IBAction public func hideOrShowRandomRow() {
46+
let randomRow = Int.random(in: 0..<stackView.rows.count)
47+
let newRowStatus = !stackView.rows[randomRow].isHidden
48+
stackView.setRowHidden(index: randomRow, isHidden: newRowStatus, animated: true)
49+
}
50+
51+
@IBAction public func moveRowToRandom() {
52+
let randomSrc = Int.random(in: 0..<stackView.rows.count)
53+
let randomDst = Int.random(in: 0..<stackView.rows.count)
54+
stackView.moveRow(index: randomSrc, to: randomDst, animated: true, completion: nil)
55+
}
56+
57+
@IBAction public func removeRow() {
58+
let randomRow = Int.random(in: 0..<stackView.rows.count)
59+
stackView.removeRow(index: randomRow, animated: true)
60+
}
61+
62+
@IBAction public func toggleAxis() {
63+
stackView.toggleAxis(animated: true)
64+
}
65+
66+
@IBAction public func scrollToRandom() {
67+
let randomRow = Int.random(in: 0..<stackView.rows.count)
68+
stackView.scrollToRow(index: randomRow, at: .middle, animated: true)
69+
}
5770
}
5871

5972
extension ViewController: TagsVCProtocol {

Sources/ScrollStackController/ScrollStack.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
380380
}
381381
stackView.insertArrangedSubview(sourceRow, at: destIndex)
382382
postInsertRow(sourceRow, animated: false)
383+
stackView.setNeedsLayout()
383384
}
384385

385386
guard animated else {
@@ -388,7 +389,6 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
388389
return
389390
}
390391

391-
stackView.setNeedsLayout()
392392
UIView.execute(executeMoveRow, completion: completion)
393393
}
394394

@@ -538,6 +538,16 @@ open class ScrollStack: UIScrollView, UIScrollViewDelegate {
538538
}
539539
}
540540

541+
/// Invert axis of scroll.
542+
///
543+
/// - Parameter animated: `true` to animate operation.
544+
/// - Parameter completion: completion callback.
545+
open func toggleAxis(animated: Bool = false, completion: (() -> Void)? = nil) {
546+
UIView.execute(animated: animated, {
547+
self.axis = (self.axis == .horizontal ? .vertical : .horizontal)
548+
}, completion: completion)
549+
}
550+
541551
// MARK: - Private Functions
542552

543553
/// Initial configuration of the control.

0 commit comments

Comments
 (0)