Clipped Image calls TapAction outside frame

前端 未结 1 769
轻奢々
轻奢々 2021-01-13 11:01

I have a issue concerning the tapAction on a image. The TapAction closure gets called on the clipped area which shouldn\'t happen. What should I do?

Image(ui         


        
相关标签:
1条回答
  • 2021-01-13 12:04

    Update

    I updated my answer. This is the proper way of doing it. There is a modifier called contentShape() that you can use to define the hit test area:

    import SwiftUI
    
    struct ContentView: View {
        @State private var tapped = false
    
        var body: some View {
            Image(systemName: "circle.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(height: 200, alignment: .center)
                .presentation(tapped ? Modal(Image(systemName: "photo")) : nil)
                .clipped()
                .cornerRadius(10)
                .border(Color.black, width: 2, cornerRadius: 10)
                .contentShape(TapShape())
                .tapAction {
                    self.tapped.toggle()
                }
        }
    
        struct TapShape : Shape {
            func path(in rect: CGRect) -> Path {
                return Path(CGRect(x: 0, y: 0, width: rect.width, height: 200))
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题