![[SwiftUI Masterclass 2023] Section 7 정리](https://velog.velcdn.com/images/youz2me/post/7345c9d2-be89-4619-8496-7f06024e5d2d/image.png)
.onTapGesture(count: 2, perform: { if imageScale == 1 { // 확대되지 않은 상태면 withAnimation(.spring()) { imageScale = 5 // Scale up } } else { // 확대된 상태면 resetImageState() // 위치와 크기 원래 상태로 되돌림 } })
UIKit에서도 터치 이벤트를 다루는 메소드가 있었는데 SwiftUI에서는 .onTapGesture 가 그 역할을 한다. 파라미터로는 count 와 perform 이 있는데, count 는 view 를 몇 번 터치했는지, perform 은 그 경우 발생하는 action 을 정의한다.
.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.