SwiftUI touch영역 풀로 잡기

YSYD·2021년 9월 13일
0

SwiftUI

목록 보기
3/3
  • HStack 영역을 터치했을 때 액션을 구현하기 위해 HStack 안에 Image랑 Text를 넣고 HStack자체에 onTapGesture를 넣어주었다.
VStack {
    Image(systemName: "person.circle").resizable().frame(width: 50, height: 50)
    Spacer().frame(height: 50)
    Text("Paul Hudson")
}
.onTapGesture {
    print("Show details for user")
}
  • 근데 HStack 영역 전체가 아닌 Image랑 Text를 터치했을 때에만 onTapGesture가 트리거 되었다...
  • 찾아보니 영역전체에서 터치이벤트를 받으려면 .contentShape(Rectangle()) 처리를 해주면 된다고함. (Stack내부에 있는 컨텐츠에만 터치이벤트를 받는 것이 디폴트 동작)
VStack {
    Image(systemName: "person.circle").resizable().frame(width: 50, height: 50)
    Spacer().frame(height: 50)
    Text("Paul Hudson")
}
.contentShape(Rectangle()) // 추가
.onTapGesture {
    print("Show details for user")
}

잘된다 contentShap을 좀 봐볼까.

.contentShape

hit testing을 위한 content shape을 정의

contentShape(_:eoFill:)

  • Return Value
    A view that uses the given shape for hit testing.
  • Parameters
    shape : The hit testing shape for the view.
    eoFill : A Boolean that indicates whether the shape is interpreted with the even-odd winding number rule.
func contentShape<S>(_ shape: S, eoFill: Bool = false) -> some View where S : Shape
  • even-odd winding number rule..? 이 뭔지 정확히는 모르겠으나 지정해준 shape의 패스선이 서로 겹치는 부분에서 내부규칙을 even-odd winding number 규칙으로 사용할건지 여부를 결정하도록 옵션을 줄수 있는거같다.
profile
YSYD

0개의 댓글