[SwiftUI] UIViewRepresentableLanguage/Swift2022. 9. 20. 14:00
Table of Contents
UIViewRepresentable
: A wrapper for a UIKit view that you use to integrate that view into your SwiftUI view hierarchy.
Use this protocol to port a UIView from UIKit into SwiftUI
Setup
To conform to UIViewRepresentable, you must implement four main lifecycle functions:
- makeUIView(context:): Create and return an instance of your UIView Type here.
- updateUIView(_:context:): This is immediately called once after the call to makeUIView(context:), then called whenever any state changes.
- dismantleUIView(_:coordinator:): Upon destruction of the parent container, this gets called. (This is optional. A default implementation is provided.)
- makeCoordinator(): This creates a Coordinator for the view. (This is also optional.)
// ActivityIndicator.swift
struct ActivityIndicator: UIViewRepresentable {
typealias Context = UIViewRepresentableContext<Self>
typealias UIViewType = UIActivityIndicatorView
public func makeUIView(context: Context) -> UIViewType {
UIActivityIndicatorView(style: .medium)
}
public func updateUIView(_ uiView: UIViewType, context: Context) {
if context.environment.isEnabled && !uiView.isAnimating {
uiView.startAnimating()
}
if !context.environment.isEnabled && uiView.isAnimating {
uiView.stopAnimating()
}
}
}
// ContenView.swift
struct ContentView: View {
@State var isAnimating: Bool = false
var body: some View {
VStack {
Toggle("Animating", isOn: $isAnimating)
ActivityIndicator()
.disabled(!isAnimating)
}
}
}
참고사이트
'Language > Swift' 카테고리의 다른 글
[swift] Pod? Cocoapod install & remove (0) | 2022.10.05 |
---|---|
[SwiftUI] Naver Map 처음 위치, 현재 위치, Maker (0) | 2022.09.26 |
[SwiftUI] Naver Map iOS install (0) | 2022.09.20 |
[SwiftUI] Image (0) | 2022.09.15 |
[SwiftUI] Text, custom font (0) | 2022.09.14 |
@jaewpark :: 코스모스, 봄보다는 늦을지언정 가을에 피어나다
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!