23.12.15 TIL SwiftUI 14. Implementing a ScrollView & Lazy Stacks

Hay·2023년 12월 15일
0

SwiftUI_Beginner

목록 보기
10/19

Implementing a ScrollView & Lazy Stacks

  • 스크롤링 기능 넣는것 배움
  • 그리고 Lazy Stacks를 이용해서, 아이폰의 화면에 나오지 않는 부분들은 스크롤링하면서 필요할때만 로딩하는 기능을 배움. 데이터세트가 클때는 이 방법을 이용한다고 함
import SwiftUI

struct ScrollViewBootcamp: View {
    var body: some View {
        //Scroll View & Lazy Stacks
        ScrollView {
            //LazyVStack은 전체를 다 로드하는게 아니라, 화면에 보이는 만큼만(필요한 만큼만) 로드한다
            //나머지 보이지 않는 부분은 creating as we scroll through.
            //this makes the app more efficent and also avoid downloading all excess data.
            //따라서 Large data set을 이용할때는 가능하다면 Lazy를 이용하는것 ㅊㅊ
            LazyVStack {
                ForEach(0..<100) { index in
                    ScrollView(.horizontal, showsIndicators: false, content: {
                        LazyHStack {
                            ForEach(0..<20) { index in
                                RoundedRectangle(cornerRadius: 25.0)
                                    .fill(Color.white)
                                    .frame(width: 200, height: 150)
                                    .shadow(radius: 10)
                                    .padding()
                                //이렇게 하게 되면 사각형을 위아래, 옆으로 스크롤할 수 있게 된다
                            }
                            
                        }
                    })
                   
                }
            }
        }
        
//-------------------------------------------------------------        
        //예시 1
        //showsIndicators 를 통해서 스크롤할때 옆에 인디케이터를 보여줄수도 있고 안보여줄수도 있디.
        ScrollView(.horizontal, showsIndicators: true, content: {
            HStack {
                ForEach(0..<50) { index in
                    Rectangle()
                        .fill(Color.blue)
                        .frame(width: 300, height: 300)

                }
        }
       
        })
    }
}

#Preview {
    ScrollViewBootcamp()
}

0개의 댓글