_Rey
프로그래뭄
_Rey
전체 방문자
오늘
어제
  • 분류 전체보기 (118)
    • Life (2)
    • iOS (49)
      • iOS (13)
      • Swift (19)
      • UIKit (0)
      • RxSwift (6)
      • SwiftUI (11)
    • Design Pattern (14)
      • GoF - Creational Patterns (6)
      • GoF - Structural Patterns (7)
    • Data Structure & Algorithm (48)
      • Data Structure (3)
      • Algorithm (8)
      • Advent of Code 2024 (1)
      • 코테 스터디 TIL (36)
    • English (2)
    • Book (2)
      • Clean Architecture (2)

블로그 메뉴

  • Instagram
  • Github
  • Naver Blog

공지사항

  • Hello, Programoom

인기 글

hELLO · Designed By 정상우.
_Rey

프로그래뭄

iOS/SwiftUI

[SwiftUI] @GestureState, @Namespace, @ScaledMetric

2022. 4. 19. 10:59

@GestureState

Gesture와 관련된 Wrapper다.

struct SimpleLongPressGestureView: View {
    @GestureState var isDetectingLongPress = false

    var longPress: some Gesture {
        LongPressGesture(minimumDuration: 3)
            .updating(self.$isDetectingLongPress) { currentState, gestureState, transaction in
                gestureState = currentState
            }
    }

    var body: some View {
        Circle()
            .fill(self.isDetectingLongPress ? Color.red : Color.green)
            .frame(width: 100, height: 100, alignment: .center)
            .gesture(longPress)
    }
}

// Example 2
@GestureState var dragAmount = CGSize.zero
Rectangle()
	.fill(.cyan)
	.frame(width: 100, height: 100)
	.offset(self.dragAmount)
	.gesture(
		DragGesture()
			.updating(self.$dragAmount) { value, state, transaction in
				state = value.translation
			}
	)

State가 붙어있는 것처럼 값 변화에 따라서 View가 업데이트된다. Binding은 Gesture 안에서 하게 된다.

 

@Namespace

애니메이션을 하나의 그룹으로 묶는다고 이해하면 편하다. matchedGeometryEffect 메소드와 함께 쓰인다.

// 부모 뷰
@Namespace var namespace
...
자식뷰(namespace: self.namespace)

// 자식 뷰
let namespace: Namespace.ID
...
	.matchedGeometryEffect(id: "{NAME_SPACE_KEY}", in: namespace)

키값으로 애니메이션이 묶이면서 더 자연스러운 애니메이션이 작동한다.

 

@ScaledMetric

사용자 폰트 크기 등 사용자가 설정 시스템 크기를 반영할 수 있다.

struct ScaledMetricView: View {
    @ScaledMetric(relativeTo: .largeTitle) var fontSize: CGFloat = 20
    @ScaledMetric(relativeTo: .largeTitle) var imageSize: CGFloat = 100
    var body: some View {
        VStack{
            Text("cloud.sun.bolt.fill")
                .font(.system(size: self.fontSize, weight: .regular, design: .default)  )
            Image(systemName: "cloud.sun.bolt.fill")
                .resizable()
                .frame(width: self.imageSize, height: self.imageSize)
        }
    }
}

 

저작자표시 비영리 변경금지 (새창열림)

'iOS > SwiftUI' 카테고리의 다른 글

[SwiftUI] User Interface Elements  (0) 2022.04.24
[SwiftUI] View Containers  (0) 2022.04.24
[SwiftUI] @UIApplicationDelegateAdaptor, @NSApplicationDelegateAdaptor  (0) 2022.04.19
[SwiftUI] @Environment, @EnvironmentObject, @FocusedBinding, @FocusedValue  (0) 2022.04.19
[SwiftUI] @Published, @ObservedObject, @StateObject  (0) 2022.04.19
    'iOS/SwiftUI' 카테고리의 다른 글
    • [SwiftUI] User Interface Elements
    • [SwiftUI] View Containers
    • [SwiftUI] @UIApplicationDelegateAdaptor, @NSApplicationDelegateAdaptor
    • [SwiftUI] @Environment, @EnvironmentObject, @FocusedBinding, @FocusedValue
    _Rey
    _Rey
    잘 배워서 다 남주자

    티스토리툴바