본문 바로가기

학습 노트

(176)
30. Gesture Tap Gesture .onTapGesture Apple Developer Documentation developer.apple.com struct TapGesture_Tutorials: View { @State private var tapCount = 0 var body: some View { VStack { Text("\(tapCount)") .font(.system(size: 250)) HStack { Image(systemName: "minus.circle") .font(.system(size: 100)) .foregroundColor(.red) .padding() .onTapGesture { tapCount -= 1 } Image(systemName: "plus.circle") .font(.sy..
29. List의 부가 기능 구현하기 List의 부가 기능 구현하기 Pull Refresh Apple Developer Documentation developer.apple.com 화면을 아래로 끌어당겨 새로고침 하는 iOS의 가장 보편적인 새로고침 방식이다. 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) } .animation(.easeInOut, value: items) .refreshable { await refresh() } } private func refresh(..
28. ForEach & Grid ForEach Apple Developer Documentation developer.apple.com struct View_ForEach: View { var items = AppleProduct.sampleList var body: some View { VStack { ForEach(items, id: \.name) { item in Text(item.name) } } } } 중간중간 등장했던 ForEach는 List나 이후에 살펴볼 Grid에서 폭넓게 사용된다. List와 마찬가지로 데이터를 나열한다는 점에서든 동일하지만, Table에 표시하지 않고 데이터들을 '나열' 하기만 한다. ForEach를 표시하는 형식은 ForEach가 어느 View에 Embed 됐는지가 기준이 됨을 기억하자. List의..
27. List #2 List #2 Selection Apple Developer Documentation developer.apple.com 일반 모드 struct SingleSelection: View { var items = AppleProduct.sampleList @State private var selected: AppleProduct? = nil var body: some View { VStack { Text("Selected item: \(selected?.name ?? "_")") List(items) { item in Button { selected = item } label: { Text(item.name) } } } } } 일반적인 List에 Selection 기능을 구현한 경우이다. 특별한 생성자의 사..
26. List #1 List #1 List는 여러 데이터를 각각의 열로써 수직으로 배치하는 View다. 애플의 모든 플랫폼을 지원하며, 각각에 알맞게 표시한다. SwiftUI의 List는 UIKit의 TableView와 같은 기능을 한다. StaticList struct StaticList: View { var body: some View { List { HStack { Text("Hello, World!") Text("Hello, World!") } Text("Hello, World!") Image(systemName: "star") Toggle(isOn: .constant(true)) { Text("On") } } } } List에 Embed 된 View를 각각의 개별 Cell로 구성한다. DynamicList stru..
25. StateObject @StateObject Apple Developer Documentation developer.apple.com struct StateObject_Tutorials: View { @State private var color: Color = Color.gray var body: some View { VStack { NumberView() .frame(width: 200, height: 200) .background(color) .clipShape(Circle()) Button { color = Color(white: Double.random(in: 0.5 ... 1.0), opacity: 1.0) } label: { Text("Change Color") } .padding() } } } 코드는 'Chang..
24. Observable Object & Environment Object Observable Object Apple Developer Documentation developer.apple.com ObservableObject는 ObservableObject, ObservedObject, Published 셋으로 이루어져 동작한다. Observable Object Class 프로토콜로 View에서 인스턴스의 변화를 감시 가능하다. 값이 바뀌면 View의 업데이트가 가능해진다. 주로 View 모델, 공유 데이터를 구성한다. Observed Object Property Wrapper이다. ObservableObject를 감시한다. Published Property Wrapper이다. ObservableObject에서 사용한다. 다른 View에서 해당 속성을 감시할 수 있다. cla..
23. State & Binding UIKit SwiftUI UIKit과 SwiftUI의 가장 큰 차이점은 View(UI)를 직접 업데이트하지 않는다는 점이다. SwiftUI에서 View는 상태(State)에 자동으로 반응한다. 이러한 상태를 나타내는 변수를 Property Wrapper 라고 부르며 자주 사용되는 것들은 다음과 같다. State Binding Environment ObservedObject EnvironmentObject StateObject All SwiftUI property wrappers explained and compared - a free SwiftUI by Example tutorial Was this page useful? Let us know! 1 2 3 4 5 www.hackingwithswift.co..
22. NavigationView & TabView NavigationView Apple Developer Documentation developer.apple.com 정리하는 시점에는 이미 사용이 중단돼 NavigationStack으로 대체됐다. NavigationStack Apple Developer Documentation developer.apple.com 대부분의 modifier는 그대로 사용할 수 있으니 글은 NavigationView를 기준으로 정리하고, 변경된 부분에 대한 설명이 필요할 경우 추가 언급하도록 한다. 앱을 사용하다 보면 아래에서 올라오는 게 아닌, 옆으로 넘어가는 전환 효과와 함께 새로운 화면을 표시하는 경우가 있다. 새로운 화면을 표시하는 과정(왼쪽)을 'Push'라고 부르고, 이전의 화면으로 돌아가는 과정(오른쪽)을 'Po..
SwiftUI에서 키보드 숨기기 SwiftUI가 버전업 되면서 여러 기능이 추가되는 가운데 여전히 지원하지 않는 기능은 Responder에 관한 제어 권한이다. 별도의 변수를 생성해 이를 이용해 제어하는 경우가 많은데, 그런 거창한 거 필요 없이 해제를 하고자 하는 경우가 있다. #if canImport(UIKit) extension View { func hideKeyboard() { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } #endif 간단하게 UIKit에 정의된 Responder 메서드를 불러와 파라미터를 전부 비워주면 responder가 해제된다. 위와 같이 별도의 함수로 선..