swiftPunk
swiftPunk

Reputation: 1

SwiftUI bug or code issue with onTapGesture combine with other View?

I want SwiftUI could understand where I am tapping on my app, therefore I gave .onTapGesture possibility to my Views, in this codes a View is bigger than its parent View but I could fix the issue with clip, the issue get fixed visually, but in codes SwiftUI see the Rec in original size, I wanted to know if this miss-behaviour is a bug in SwiftUI, or code we solve it with some magic codes? thanks for all.

gif:

see the gif of issue

struct ContentView: View {
var body: some View {
    
    ZStack {
        
        Color.white.ignoresSafeArea()
            .onTapGesture { print("you tapped on Screen!") }
        
        
        Circle()
            .fill(Color.red)
            .frame(width: 300, height: 300, alignment: .center)
            .overlay(
                
                Rectangle()
                    .fill(Color.yellow)
                    .frame(width: 100, height: UIScreen.main.bounds.height, alignment: .center)
                    .onTapGesture { print("you tapped on Rectangle!") }
                
            )
            .clipShape(Circle())
            .onTapGesture { print("you tapped on Circle!") }
        
    }
    
    
    }
}

Upvotes: 2

Views: 218

Answers (1)

davidev
davidev

Reputation: 8517

Just use contentShape(Circle()) before the onTapGesture and it is fixed

Circle()
    .fill(Color.red)
    .frame(width: 300, height: 300, alignment: .center)
    .overlay(                
        Rectangle()
            .fill(Color.yellow)
            .frame(width: 100, height: UIScreen.main.bounds.height, alignment: .center)
            .onTapGesture { print("you tapped on Rectangle!") }
    )
    .clipShape(Circle())
    .contentShape(Circle()) //<< contentShape here 
    .onTapGesture { print("you tapped on Circle!") }

Upvotes: 1

Related Questions