Notice
Recent Posts
Recent Comments
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[SwiftUI] lazyVGrid ๋ณธ๋ฌธ

apple/๐Ÿš SwiftUI & Combine

[SwiftUI] lazyVGrid

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 5. 25. 17:20

lazyVGrid

 

โœ… swiftUI ์•„์ง๋„ ๋‹ค๋ฃจ๋Š”๊ฒŒ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค.

UIKit์ด์—ˆ๋‹ค๋ฉด ๋”ฑ ๋˜์–ด์•ผ ํ•˜๋Š”๊ฑด๋ฐ, ์™œ ์•ˆ๋˜์ง€ ํ•˜๋Š”๊ฑธ ๋ณด๋‹ˆ๊นŒ ์ˆ™๋ จ๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ ๊ฐ™์•„์„œ ๊ฐ‘์ž๊ธฐ ์Šฌํ”„๋‹ค ใ… ใ… 

 

๋˜ ๋Š๋‚€์ ์ธ๋ฐ ์Šค์œ ๊ฐ€ ๋š๋”ฑ๋š๋”ฑ ์ฒ˜์Œ์— ํ•œ 80ํ”„๋กœ๊นŒ์ง€๋Š” ๋นจ๋ฆฌ ๋งŒ๋“œ๋Š”๋ฐ ๊ฒฐ๊ตญ์€ UIKit ๋ถ€๋ถ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์‹œ์ ์—์„œ๋Š” ์†์ด ๋งŽ์ด๊ฐ€๊ธฐ ์‹œ์ž‘ํ•ด์„œ, ํ ,,

 

โœ… ์ฝ”๋“œ

import Foundation
import SwiftUI

enum LayoutType: CaseIterable {
    case table, grid, multiple
}

extension LayoutType {
    // ๋ ˆ์ด์•„์›ƒ ํƒ€์ž…์— ๋Œ€ํ•œ ์ปฌ๋Ÿผ์ด ์ž๋™์œผ๋กœ ์„ค์ •๋˜๋„๋ก ํ•œ๋‹ค
    var columns : [GridItem] {
        switch self {
        case .table:
            return [
                // flexible ํ•˜๋‚˜๋กœ ํ•œ์ค„๋กœ ํ‘œํ˜„
                GridItem(.flexible())
            ]
        case .grid:
            return [
                // flexible ๋‘๊ฐœ๋ฅผ ๋„ฃ์–ด์„œ ๋‘๊ฐœ ์•„์ดํ…œ ๋“ค์–ด๊ฐ€๊ฒŒ ํ•จ
                GridItem(.flexible()),
                GridItem(.flexible())
            ]
        case .multiple:
            return [
                // ์–ด๋‹ตํ‹ฐ๋ธŒ๋ฅผ ํ†ตํ•ด ํฌ๊ธฐ ๋‹ฟ๋Š”๋ฐ ๊นŒ์ง€ ์•„์ดํ…œ ์—ฌ๋Ÿฌ๊ฐœ ๋„ฃ๊ธฐ
                GridItem(.adaptive(minimum: 100))
            ]
        }
    }
}

struct SegmentLayoutView: View {
    
    var dummyDatas = MyModel.dummyDataArray
    
    @State var selectedLayoutType: LayoutType = .table
    
    var body: some View{
        VStack{
            // ํ”ผ์ปค
            Picker(selection: $selectedLayoutType, label: Text("๋ ˆ์ด์•„์›ƒ ํƒ€์ž…"), content: /*@START_MENU_TOKEN@*/{
                ForEach(LayoutType.allCases, id: \.self , content: { layoutType in
                    switch layoutType {
                    case .table:
                        Image(systemName: "list.dash")
                    case .grid:
                        Image(systemName: "square.grid.2x2.fill")
                    case .multiple:
                        Image(systemName: "circle.grid.3x3.fill")
                    }
                })
            }/*@END_MENU_TOKEN@*/)
            .frame(width: 250)
            .pickerStyle(SegmentedPickerStyle())
            // ๋‚ด์šฉ๋ฌผ
            ScrollView{
                LazyVGrid(columns: selectedLayoutType.columns, content: /*@START_MENU_TOKEN@*/ {
                
                	// Contents์ชฝ์— switch๋ฌธ ๋‹ฌ์•„์„œ view์ „ํ™˜
                    ForEach(dummyDatas) { dataItem in
                        switch selectedLayoutType {
                        case .table:
                            Card(icon: "book.fill", title: "์ฑ…์ฝ๊ธฐ", start: "1 PM", end: "3 PM", bgColor: Color.green)
                        case .grid:
                                RoundedRectangle(cornerRadius: 25.0)
                                    .foregroundColor(Color.init(#colorLiteral(red: 1, green: 0.5758225922, blue: 0.3784928128, alpha: 1)))
                                    .frame(height: 200)
                                    .overlay(
                                        VStack(spacing: 2){
                                            Circle().frame(height: 100)
                                                .foregroundColor(.yellow)
                                            Spacer().frame(height: 10)
                                            Text("\(dataItem.title)")
                                                .font(.system(size: 20))
                                                .fontWeight(.bold)
                                            Text("\(dataItem.content)")
                                        }
                                    )
                        case .multiple:
                            Rectangle()
                                .foregroundColor(.blue)
                                .frame(height: 100)
                        }
                    }
                    
                }/*@END_MENU_TOKEN@*/)
                .animation(.easeInOut) // ํšจ๊ณผ ๋‹ฌ์•„์ฃผ๊ธฐ
                .padding(.horizontal, 10)
            }
        }
    }
}

 

 

 

๊ฒฐ๊ณผ๋ฌผ UI

 

 

 

โœ… ๋‚ด๊ฐ€ ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด์ž

import SwiftUI

enum LayoutType: CaseIterable {
    case table, grid, multiple
    
    var columns: [GridItem] {
        switch self {
        case .table:
            return [GridItem(.flexible())]
        case .grid:
            return [
                GridItem(.flexible()),
                GridItem(.flexible())
            ]
        case .multiple:
            return [GridItem(.adaptive(minimum: 100))]
        }
    }
}

struct HomeView: View {
    
    @State var selectedLayoutType: LayoutType = .table
    
    var body: some View {
        VStack(spacing: 0) {
            Picker("", selection: $selectedLayoutType) { // picker๋ฅผ ์Šค์œ„์น˜๋ฌธ์œผ๋กœ ๊ด€๋ฆฌ
                ForEach(LayoutType.allCases, id: \.self) {
                    switch $0 {
                    case .table: Image(systemName: "list.dash")
                    case .grid: Image(systemName: "square.grid.2x2.fill")
                    case .multiple: Image(systemName: "circle.grid.3x3.fill")
                    }
                }
                
            }
            .pickerStyle(.segmented)
            .padding()
            
            Spacer()
            
            ScrollView {
                LazyVGrid(
                    columns: selectedLayoutType.columns,
                    spacing: 5
                ) {
                    ForEach((0...30), id: \.self) { index in
                        switch selectedLayoutType { // ์Šค์œ„์น˜๋ฌธ!
                        case .table:
                            RoundedRectangle(cornerRadius: 25.0)
                                .frame(height: 100)
                                .foregroundColor(.blue)
                                .padding([.leading, .trailing], 10)
                        case .grid:
                            RoundedRectangle(cornerRadius: 25.0)
                                .frame(height: 200)
                                .foregroundColor(.blue)
                                .overlay(
                                    VStack(spacing: 2){
                                        Circle()
                                            .frame(height: 100)
                                            .foregroundColor(.yellow)
                                        Spacer().frame(height: 10)
                                        Text("\(index)")
                                            .font(.system(size: 20))
                                            .fontWeight(.bold)
                                        Text("\(index)")
                                    }
                                )
                            
                        case .multiple:
                            Rectangle()
                                .foregroundColor(.blue)
                                .frame(height: 100)
                            
                            
                        }
                    }
                }
                .animation(.easeInOut)
                .padding(.horizontal, 10)
            }
            
            Spacer()
        }
    }
    
    
}

struct HalfSheet_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}



extension Color {
    static func random() -> Color {
        let random = Double(CGFloat(arc4random()) / CGFloat(UInt32.max))
        return Color(
            red:   random,
            green: random,
            blue:  random
        )
    }
}
Comments