본문 바로가기
PROGRAMMING CODE/SWIFT

[SwiftUI] highPriorityGesture (NavigationLink 위 제스쳐 처리)

by daye_ 2023. 10. 10.

 
 
드래그로 필터 바꾸는 코드 짜고있었는데
List들이 NavigationLink로 이루어져 있어서 드래그 대신 링크로 이동하거나 드래드가 인식되지 않았음


VStack{
	...
}
.gesture(
                DragGesture(minimumDistance: 2, coordinateSpace: .local)
                    .onEnded { value in
                        if ((selectedFilter == .group) && (value.translation.width > 0)) {
                            withAnimation {
                                selectedFilter = .personal
                            }
                        }
                        else if ((selectedFilter == .personal) && (value.translation.width < 0)) {
                            withAnimation {
                                selectedFilter = .group
                            }
                        }
                    }
            )

 누가 멋진걸 알려주어서 써 보았다 ^.^



 
 
highPriorityGesture는 뷰에 의해 정의된 제스처보다 우선 순위가 높은 제스처를 알려줌!
 
 

 
 
 


VStack{
	...
}
.highPriorityGesture(
                DragGesture(minimumDistance: 2, coordinateSpace: .local)
                    .onEnded { value in
                        if ((selectedFilter == .group) && (value.translation.width > 0)) {
                            withAnimation {
                                selectedFilter = .personal
                            }
                        }
                        else if ((selectedFilter == .personal) && (value.translation.width < 0)) {
                            withAnimation {
                                selectedFilter = .group
                            }
                        }
                    }
            )

여기는 따로 DragGesture 외 추가할 gesture가 없었기 때문에 바로 넣어주었음

NavigationLink, DragGesture 둘 다 잘 작동
 
 
 
 
 
 
아래와 같이 클로저를 이용해서 넣어줄 수도 있음!


let newGesture = TapGesture().onEnded {
        print("Tap on VStack.")
    }
    
    ...
    
    
VStack{
    ...
}
 .highPriorityGesture(newGesture)

 
 
 
 
https://developer.apple.com/documentation/swiftui/view/highprioritygesture(_:including:) 

highPriorityGesture(_:including:) | Apple Developer Documentation

Attaches a gesture to the view with a higher precedence than gestures defined by the view.

developer.apple.com