SwiftUI에서 UIView와 연동하여 기능을 구현해야하는 경우가 있다. 이 경우 UIViewRepresentable 프로토콜을 이용하면 된다. 공식 문서를 살펴보자.
https://developer.apple.com/documentation/swiftui/uiviewrepresentable
A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy.
UIViewRepresentable 인스턴스를 만들면 UIView 객체를 SwiftUI 인터페이스에서 제어할 수 있다. 생성과 업데이트는 SwiftUI 뷰와 동시에 이루어지며, 앱의 현재 상태 정보를 확인, 제어할 수 있다. 또한 teardown 프로세르를 통해 뷰가 SwiftUI에서 완전히 지워지게 할 수도 있다. 시스템은 자동적으로 다른 뷰와의 상호작용을 허용하지는 않으므로 이 경우 Coordinator을 이용하면 된다.
중요해 보이는 몇 가지 내용을 더 살펴보자
func makeUIView(context: Self.Context) -> Self.UIViewType
Creates the view object and configures its initial state.
Required.
makeUIView를 통해 뷰 객체를 초기화하고 생성한다.
func updateUIView(Self.UIViewType, context: Self.Context)
Updates the state of the specified view with new information from SwiftUI.
Required.
updateUIView를 통해 새로운 정보를 토대로 뷰를 업데이트한다.
func makeCoordinator() -> Self.Coordinator
Creates the custom instance that you use to communicate changes from your view to other parts of your SwiftUI interface.
Required. Default implementation provided.
SWiftUI의 다른 뷰와 상호작용하기 위해 사용된다.
'SwiftUI > 기타' 카테고리의 다른 글
SwiftUI 로 Firebase 이용하는 기본적인 방법 (0) | 2021.08.14 |
---|---|
SwiftUI 내장 이미지 이용하기 (0) | 2021.08.10 |