Language/Swift

SwiftUI를 이용한 Gesture 활용하기

IFLA 2022. 11. 8. 11:38

SwiftUI는 뷰 작업을 위한 많은 제스처를 제공한다. 중요한 부분에 집중할 수 있도록 대부분의 수고를 덜어준다. 가장 흔한 제스처는 onTapGesture()다.

기본코드

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .onTapGesture(count: 2) {
                print("Dobule Tapped!")
            }
    }
}

 

실행화면

 

주요 제스처

  • LongPressGesture : 사용자가 지정한 시간 이상 View를 누르고 있느 경우 인식한다.
  • DragGesture : 뷰의 자연스러운 위치를 조절할 수 있는 수정자와 결합할 때 좋다.
  • MagnificationGesture : 사진앱이나 인스타그램, 페이스북에서 사진을 확대 축소할 때 사용한다.

 

ManificationGesture 사용 코드

import SwiftUI

struct ContentView: View {
    @State var currentAmount: CGFloat = 0
    @State var lastAmount: CGFloat = 1

    var body: some View {
        Text("MagnificationGesture")
            .font(Font.title.bold())
            .foregroundColor(.white)
            .padding(50)
            .background(Color.blue.cornerRadius(20))
            .scaleEffect(currentAmount + lastAmount)
            .gesture(
                MagnificationGesture()
                    .onChanged { value in
                        currentAmount = value - 1
                    }
                    .onEnded { value in
                        lastAmount += currentAmount
                        currentAmount = 0
                    }
            )
    }
}

 

 


개발자 Document

How to use gestures in SwiftUI