Youjin Lee.

[SwiftUI Masterclass 2023] Section 7 정리

[SwiftUI Masterclass 2023] Section 7 정리

SwiftUI 공부하기

Gesture

OnTapGesture

.onTapGesture(count: 2, perform: { if imageScale == 1 { // 확대되지 않은 상태면 withAnimation(.spring()) { imageScale = 5 // Scale up } } else { // 확대된 상태면 resetImageState() // 위치와 크기 원래 상태로 되돌림 } })

UIKit에서도 터치 이벤트를 다루는 메소드가 있었는데 SwiftUI에서는 .onTapGesture 가 그 역할을 한다. 파라미터로는 countperform 이 있는데, countview 를 몇 번 터치했는지, perform 은 그 경우 발생하는 action 을 정의한다.

MagnificationGesture

.gesture( MagnificationGesture() .onChanged { value in withAnimation(.linear(duration: 1)) { if imageScale >= 1 && imageScale <= 5 { imageScale = value } else if imageScale > 5 { imageScale = 5 } } } .onEnded { _ in if imageScale > 5 { imageScale = 5 } } )

이미지를 손가락으로 확대하거나 축소할 때 사용한다. 사용자가 화면과의 상호작용을 하게 되면 .onChange() 안에 있는 구문이 실행된다.

© 2025 Youjin Lee. All rights reserved.