CardView is a versatile SwiftUI component for displaying a collection of views in a card-like format. It supports both grid and horizontal scrolling layouts and includes optional scaling effects
- Display views in a grid or horizontal scrolling layout
- Optional scaling effect for views based on their position
- Can display views in carousel format with optional scaling
- Customizable view dimensions and spacing
- Accepts an array of content views
- Display cards in carousel
- Change the number of rows as per requirement
- Based on the number rows selected the the content in the array gets adjusted
- Add scaling effect
![Screenshot 2024-07-09 at 6 40 04 PM](https://private-user-images.githubusercontent.com/139471661/346968664-14ad15a0-780f-4bf1-a3a6-c1ae7983c2c9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxOTAwNjcsIm5iZiI6MTczOTE4OTc2NywicGF0aCI6Ii8xMzk0NzE2NjEvMzQ2OTY4NjY0LTE0YWQxNWEwLTc4MGYtNGJmMS1hM2E2LWMxYWU3OTgzYzJjOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQxMjE2MDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zZjIzMzc4YjJhODdhMzQ2ZGEyMmMwNmQ0Y2UxZjEzOTRiYmZjZmQyZGJlMTIyMWEyZDNhYjEwN2ZhNzA5MWU4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.kdNcUWMDVubHytOUUvDMJVGv3caFmhdYQih13GRr-bw)
![Screenshot 2024-07-09 at 6 40 41 PM](https://private-user-images.githubusercontent.com/139471661/346968905-6c03de0e-3bca-4c5c-a9d0-8345b7090d66.png?jwt=eyJhbGci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C1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQxMjE2MDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00NTdkMjYxNTQwZDAyNjE0MTI4NjMwMGYxMGVjNWQ4NDA2YWM5ZTI4ZTNiN2NmYmVlZTY3ZmYxNmZlZTBmY2QyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.SP7Opjw7qHTCEweU3GQWp1Q6qXVGVQZ_-HYlYNtFbjc)
- Display cards in a grid
- Change the number of columns as per the requirement
- Based on the number of columns selected the content of the array gets adjusted
![Screenshot 2024-07-09 at 6 41 43 PM](https://private-user-images.githubusercontent.com/139471661/346969198-4d642ef8-27e4-4788-a664-107bcf9e7bba.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxOTAwNjcsIm5iZiI6MTczOTE4OTc2NywicGF0aCI6Ii8xMzk0NzE2NjEvMzQ2OTY5MTk4LTRkNjQyZWY4LTI3ZTQtNDc4OC1hNjY0LTEwN2JjZjllN2JiYS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQxMjE2MDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lYWZhNGY3NDkyMDA5OWI2OGU2Y2YxNTQwMTY5NTdjYjhiOWRkMjUwYWQyZmYzMDMxMGM3NWI1ZWQzOTdhYWE5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.XmErYqmFoNn0qnPEIY1KEM6kStiaHhoHafUpD9KQbCQ)
![Screenshot 2024-07-09 at 6 42 06 PM](https://private-user-images.githubusercontent.com/139471661/346969353-227c436a-3a8d-459d-befc-fba58b7326fc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxOTAwNjcsIm5iZiI6MTczOTE4OTc2NywicGF0aCI6Ii8xMzk0NzE2NjEvMzQ2OTY5MzUzLTIyN2M0MzZhLTNhOGQtNDU5ZC1iZWZjLWZiYTU4YjczMjZmYy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQxMjE2MDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05M2Q2ZDQ5NjVmYTBjYTMwMGEyMjBkY2NkMWQwOTRhOWU2YWJiZDgzMmQ5OTE0NTE0NDNlOWY4YzhiNTQ0M2ZhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.KejLSTtvZzNXDgWNIoI7K8rHc5nhv_QGGLoSOoIGysQ)
- Change the frame width as per your requirement
![Screenshot 2024-07-09 at 6 42 29 PM](https://private-user-images.githubusercontent.com/139471661/346969473-834dac49-faac-4c7e-8368-e9fb80f0cfdd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxOTAwNjcsIm5iZiI6MTczOTE4OTc2NywicGF0aCI6Ii8xMzk0NzE2NjEvMzQ2OTY5NDczLTgzNGRhYzQ5LWZhYWMtNGM3ZS04MzY4LWU5ZmI4MGYwY2ZkZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEwJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMFQxMjE2MDdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT01MDNlYTkwOGU2MmY5MDRiMjRmMTc3YWVlNTI2ZDNjMzE2YzljNTdhNzVmM2IzNDc0MDc5NzdmZDMwNTRmM2I0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.ECbdElCq1fAgTkxwBuEhZcUasjoqLy8GekeNLyzg2Ao)
- viewsArray: [Binding<[AnyView]>] - An array of views to be displayed.
- gridView: Binding - Whether to display the views in a grid layout or carousel format(horizontal scrolling)
- scale: Bool - Whether to apply a scaling effect. Default is false.
- scaleFactor: CGFloat - Adjust the amount of scaling as per your requirement. Default is 1.1
- frameWidth: CGFloat - The width of each view. Default is 150.
- frameHeight: CGFloat - The height of each view. Default is 150.
- additionalFrameHeight: CGFloat - Additional height given to the view for horizontal scrolling to avoid overlapping or breaking of view. Default is 30
- RowOrColumnNo: Binding - The number of columns required in the grid layout or the number of rows required for the horizontal scrolling layout .
- verticalSpacing: vertical spacing between the cards. Default is 10.
- horizontalSpacing: Horizontal spacing between the cards. Default is 10.
- onTap: ((Int) -> Void)? - An optional closure to handle tap gestures on the views.
-
Add the package through swift package manager to your project https://swiftpackageindex.com/softsuave-tech-matrix/adaptive_card_layout
-
Alternatively you can also install through cocoapods pod 'adaptive_card_layout'
Step 1:
- Create a custom view which you want to show in the card
import SwiftUI
struct CustomView1: View {
var text: String
var color: Color
var image: String
var body: some View {
VStack(alignment:.center) {
Text(text)
.font(.title)
.foregroundColor(.white)
Image(systemName: image)
.resizable()
.frame(width: 50, height: 50)
.foregroundColor(.yellow)
}
.frame(width: 180, height: 300)
.background(color)
.cornerRadius(10)
.padding()
}
}
Step 2:
-
Import CardViewAdvanced library in your parent view
-
Create an array of views as per your requirement
private var imageArray: [String:String] = ["CustomView 1":"magnifyingglass","CustomView 32":"moon.fill","CustomView 23":"sun.min","CustomView 33": "cloud","CustomView 4":"moon","CustomView 5":"sun.max","CustomView 6":"sun.snow", "CustomView 7":"sun.max.fill", "CustomView 8":"moon.zzz", "CustomView 92":"cloud.snow.fill", "CustomView 22":"cloud.snow", "CustomView 11":"sun.snow.fill","CustomView 12": "cloud.fog", "CustomView 21":"magnifyingglass","CustomView 2":"moon.fill","CustomView 3":"sun.min","CustomView 13": "cloud","CustomView 14":"moon","CustomView 15":"sun.max","CustomView 16":"sun.snow", "CustomView 17":"sun.max.fill", "CustomView 18":"moon.zzz", "CustomView 9":"cloud.snow.fill", "CustomView 10":"cloud.snow", "CustomView 19":"sun.snow.fill","CustomView 20": "cloud.fog"] @State private var viewsArray: [AnyView] = [] init() { _viewsArray = State(initialValue: imageArray.map { item in AnyView(CustomView1(text: item.key, color: Color.random, image: item.value)) }) }
Step 3:
- Pass the array of views in your parent view
- Set values of the params as per your requirement
CardView(viewsArray: $viewsArray, scale: true, frameWidth: 180, frameHeight: 300,additionalFrameHeight: 30, gridView: $isGridView, RowOrcolumnNo: $columnNo) { indexNo in
print(indexNo)
}
Contributions are always welcome! For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
If you have any feedback, please reach out to us at techmatrix@softsuave.com