├── .gitignore
├── .swiftpm
└── xcode
│ └── package.xcworkspace
│ └── xcshareddata
│ └── IDEWorkspaceChecks.plist
├── LICENSE.md
├── Package.swift
├── Preview.gif
├── README.md
└── Sources
└── CardStack
└── CardStack.swift
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | /.build
3 | /Packages
4 | /*.xcodeproj
5 | xcuserdata/
6 | DerivedData/
7 | .swiftpm/config/registries.json
8 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9 | .netrc
10 |
--------------------------------------------------------------------------------
/.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright 2022 NOT SO BIG TECH LIMITED
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 |
--------------------------------------------------------------------------------
/Package.swift:
--------------------------------------------------------------------------------
1 | // swift-tools-version: 5.6
2 | // The swift-tools-version declares the minimum version of Swift required to build this package.
3 |
4 | import PackageDescription
5 |
6 | let package = Package(
7 | name: "CardStack",
8 | platforms: [
9 | .iOS(.v14)
10 | ],
11 | products: [
12 | // Products define the executables and libraries a package produces, and make them visible to other packages.
13 | .library(
14 | name: "CardStack",
15 | targets: ["CardStack"]),
16 | ],
17 | dependencies: [
18 | // Dependencies declare other packages that this package depends on.
19 | // .package(url: /* package url */, from: "1.0.0"),
20 | ],
21 | targets: [
22 | // Targets are the basic building blocks of a package. A target can define a module or a test suite.
23 | // Targets can depend on other targets in this package, and on products in packages this package depends on.
24 | .target(
25 | name: "CardStack",
26 | dependencies: [])
27 | ]
28 | )
29 |
--------------------------------------------------------------------------------
/Preview.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/notsobigcompany/CardStack/cc0d8cf4f6caf4cfaf71593c84c9d21eaf9460f0/Preview.gif
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > [!WARNING]
2 | > This package is now deprecated in favour of [CardDeckPageViewStyle](https://opensource.notsobig.company/documentation/bigswiftui/carddeckpageviewstyle/) found in the new [BigUIPaging](https://github.com/notsobigcompany/BigUIPaging) package.
3 |
4 | # CardStack
5 |
6 | A SwiftUI view that arranges its children in a whimsical interactive deck of cards.
7 |
8 | 
9 |
10 | CardStack mimics the behaviour of the photo stack in iMessage, as well as the "Top Stories" in [Big News](https://bignews.app).
11 |
12 | ## Overview
13 | You create stacks dynamically from an underlying collection of data. Each item in the collection must conform to `Identifiable`.
14 |
15 | ```swift
16 | CardStack(items) { item in
17 | RoundedRectangle(cornerRadius: 20, style: .continuous)
18 | .fill(item.color)
19 | .frame(height: 400)
20 | }
21 | ```
22 |
23 | ## Supporting selections in stacks
24 | To make members of a stack selectable, install a tap gesture inside the view provider.
25 |
26 | ```swift
27 | CardStack(items) { item in
28 | RoundedRectangle(cornerRadius: 20, style: .continuous)
29 | .onTapGesture {
30 | // do something
31 | }
32 | }
33 | ```
34 |
35 | To get a stack's topmost card index, provide a binding to the `currentIndex` initialiser.
36 |
37 | ```swift
38 | @State private var currentIndex = 0
39 |
40 | CardStack(items, currentIndex: $currentIndex) { item in
41 | ...
42 | }
43 | ```
44 |
45 | ## Installation
46 | Add CardStack to your app's Package.swift file:
47 |
48 | ```swift
49 | .package(url: "https://github.com/notsobigcompany/CardStack")
50 | ```
51 |
52 | Alternatively, add it in Xcode by going to File -> Add Packages.
53 |
54 | A SwiftUI preview is available in the CardStack file itself.
55 |
56 | ## License
57 | Copyright 2022 NOT SO BIG TECH LIMITED
58 |
59 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
60 |
61 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
62 |
63 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64 |
--------------------------------------------------------------------------------
/Sources/CardStack/CardStack.swift:
--------------------------------------------------------------------------------
1 | import SwiftUI
2 |
3 | /**
4 | A SwiftUI view that arranges its children in an interactive deck of cards.
5 | */
6 | @available(*, deprecated, message: "Use CardDeckPageViewStyle in the new BigUIPaging package https://github.com/notsobigcompany/BigUIPaging")
7 | public struct CardStack: View where Data: RandomAccessCollection, Data.Element: Identifiable, Content: View {
8 | @State private var currentIndex: Double = 0.0
9 | @State private var previousIndex: Double = 0.0
10 |
11 | private let data: Data
12 | @ViewBuilder private let content: (Data.Element) -> Content
13 | @Binding var finalCurrentIndex: Int
14 |
15 | /// Creates a stack with the given content
16 | /// - Parameters:
17 | /// - data: The identifiable data for computing the list.
18 | /// - currentIndex: The index of the topmost card in the stack
19 | /// - content: A view builder that creates the view for a single card
20 | public init(_ data: Data, currentIndex: Binding = .constant(0), @ViewBuilder content: @escaping (Data.Element) -> Content) {
21 | self.data = data
22 | self.content = content
23 | _finalCurrentIndex = currentIndex
24 | }
25 |
26 | public var body: some View {
27 | ZStack {
28 | ForEach(Array(data.enumerated()), id: \.element.id) { (index, element) in
29 | content(element)
30 | .zIndex(zIndex(for: index))
31 | .offset(x: xOffset(for: index), y: 0)
32 | .scaleEffect(scale(for: index), anchor: .center)
33 | .rotationEffect(.degrees(rotationDegrees(for: index)))
34 | }
35 | }
36 | .highPriorityGesture(dragGesture)
37 | }
38 |
39 | private var dragGesture: some Gesture {
40 | DragGesture()
41 | .onChanged { value in
42 | withAnimation(.interactiveSpring()) {
43 | let x = (value.translation.width / 300) - previousIndex
44 | self.currentIndex = -x
45 | }
46 | }
47 | .onEnded { value in
48 | self.snapToNearestAbsoluteIndex(value.predictedEndTranslation)
49 | self.previousIndex = self.currentIndex
50 | }
51 | }
52 |
53 | private func snapToNearestAbsoluteIndex(_ predictedEndTranslation: CGSize) {
54 | withAnimation(.interpolatingSpring(stiffness: 300, damping: 40)) {
55 | let translation = predictedEndTranslation.width
56 | if abs(translation) > 200 {
57 | if translation > 0 {
58 | self.goTo(round(self.previousIndex) - 1)
59 | } else {
60 | self.goTo(round(self.previousIndex) + 1)
61 | }
62 | } else {
63 | self.currentIndex = round(currentIndex)
64 | }
65 | }
66 | }
67 |
68 | private func goTo(_ index: Double) {
69 | let maxIndex = Double(data.count - 1)
70 | if index < 0 {
71 | self.currentIndex = 0
72 | } else if index > maxIndex {
73 | self.currentIndex = maxIndex
74 | } else {
75 | self.currentIndex = index
76 | }
77 | self.finalCurrentIndex = Int(self.currentIndex)
78 | }
79 |
80 | private func zIndex(for index: Int) -> Double {
81 | if (Double(index) + 0.5) < currentIndex {
82 | return -Double(data.count - index)
83 | } else {
84 | return Double(data.count - index)
85 | }
86 | }
87 |
88 | private func xOffset(for index: Int) -> CGFloat {
89 | let topCardProgress = currentPosition(for: index)
90 | let padding = 35.0
91 | let x = ((CGFloat(index) - currentIndex) * padding)
92 | if topCardProgress > 0 && topCardProgress < 0.99 && index < (data.count - 1) {
93 | return x * swingOutMultiplier(topCardProgress)
94 | }
95 | return x
96 | }
97 |
98 | private func scale(for index: Int) -> CGFloat {
99 | return 1.0 - (0.1 * abs(currentPosition(for: index)))
100 | }
101 |
102 | private func rotationDegrees(for index: Int) -> Double {
103 | return -currentPosition(for: index) * 2
104 | }
105 |
106 | private func currentPosition(for index: Int) -> Double {
107 | currentIndex - Double(index)
108 | }
109 |
110 | private func swingOutMultiplier(_ progress: Double) -> Double {
111 | return sin(Double.pi * progress) * 15
112 | }
113 | }
114 |
115 | struct CardStackView_Previews: PreviewProvider {
116 |
117 | struct DemoItem: Identifiable {
118 | let name: String
119 | let color: Color
120 |
121 | var id: String {
122 | name
123 | }
124 | }
125 |
126 | struct DemoView: View {
127 | @State private var currentIndex = 0
128 | @State private var tappedIndex: Int? = nil
129 |
130 | var body: some View {
131 | let colors = [
132 | DemoItem(name: "Red", color: .red),
133 | DemoItem(name: "Orange", color: .orange),
134 | DemoItem(name: "Yellow", color: .yellow),
135 | DemoItem(name: "Green", color: .green),
136 | DemoItem(name: "Blue", color: .blue),
137 | DemoItem(name: "Purple", color: .purple),
138 | ]
139 |
140 | VStack {
141 | CardStack(colors, currentIndex: $currentIndex) { namedColor in
142 | RoundedRectangle(cornerRadius: 20, style: .continuous)
143 | .fill(namedColor.color)
144 | .overlay(
145 | Text(namedColor.name)
146 | .font(.largeTitle)
147 | .fontWeight(.bold)
148 | .foregroundColor(.white)
149 | )
150 | .frame(height: 400)
151 | .onTapGesture {
152 | tappedIndex = currentIndex
153 | }
154 | .padding(50)
155 |
156 | }
157 |
158 | Text("Current card is \(currentIndex)")
159 | if let tappedIndex = tappedIndex {
160 | Text("Card \(tappedIndex) was tapped")
161 | } else {
162 | Text("No card has been tapped")
163 | }
164 | }
165 | }
166 | }
167 |
168 | static var previews: some View {
169 | DemoView()
170 | }
171 |
172 | }
173 |
174 |
--------------------------------------------------------------------------------