[SwiftUI] Pull to Refresh

RudinP·2025년 8월 1일
0

Study

목록 보기
331/363

아래로 당기면 refresh 되도록 하는 방법

refreshable 모디파이어

struct PullToRefresh: View {
    @State private var items = AppleProduct.sampleList[0 ..< 2]
    @State private var index = 2
    
    var body: some View {
        List(items) { item in
            Text(item.name)
        }
        .refreshable {
            await refresh()
        }
    }
    
    private func refresh() async {
        do {
            try await Task.sleep(nanoseconds: 1_000_000_000 * 2)
        } catch {
            
        }
        
        guard index < AppleProduct.sampleList.count - 1 else { return }
        items.append(AppleProduct.sampleList[index])
        index += 1
    }
}

끊김없이 업데이트 하는 방법

  • 기본(애니메이션 X)

  • 애니메이션 O (easeInOut)

	var body: some View {
        List(items) { item in
            Text(item.name)
        }
        .animation(.easeInOut, value: items)
        .refreshable {
            await refresh()
        }
    }

profile
iOS 개발자가 되기 위한 스터디룸...

0개의 댓글