英文:
"Invisible" touchable area outside of real image view
问题
以下是您要翻译的内容:
"So I have this issue since a long time... I even submitted an Apple TSI but did not get an answer yet.
So in short I give an image in a view a certain height & width and because I want that the ratio stays similar it should scale up, which works. BUT now when the initial image, scaled up to fit with the width of the available space, the rest of the image which is invisible (which it should be) but not deleted! When i add a tap gesture recognizer it still recognizes a tap in the whole area of the scaled image even outside of the view itself.
Here an example code I wrote. Try it out and you will see, that the card is clickable above itself more that just a few pixels. Take for testing any image you would like that is much higher than the available space like a portrait screenshot."
struct ContentView: View {
var exampleImageName: String = "testimage"
@State var clicked: Bool = false
@State var removeImageCode: Bool = false
var body: some View {
VStack (alignment: .leading){
ZStack{
VStack (alignment: .leading, spacing: 12){
if(!removeImageCode){
Image(exampleImageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxHeight: 125)
.clipped()
}
HStack (alignment: .top){
VStack(alignment: .leading,spacing: 4) {
Text("Click on card to change color")
.font(.headline)
.fontDesign(.rounded)
Text("With the image you can also click above the card...")
.font(.system(size: 12))
.fontWeight(.regular)
.fontDesign(.rounded)
}
Spacer()
}
.padding(.horizontal)
.padding(.vertical)
}
.background(clicked ? .green : .red)
.cornerRadius(10)
.shadow(radius: 2)
}
.onTapGesture {
clicked.toggle()
}
Toggle("Remove image code", isOn: $removeImageCode)
}
.padding(.horizontal)
}
}
I tried nearly everything. Extracting, adding and removing specific styles etc. I am sure the error lies here, because when i take an image that fits in the given area from the proportions the clickable area is the area of the view as it should.
Hope I explained it understandable, else do not hesiate to ask for more information.
英文:
So I have this issue since a long time... I even submitted an Apple TSI but did not get an answer yet.
So in short I give an image in a view a certain height & width and because I want that the ratio stays similar it should scale up, which works. BUT now when the initial image, scaled up to fit with the width of the available space, the rest of the image which is invisible (which it should be) but not deleted! When i add a tap gesture recognizer it still recognizes a tap in the whole area of the scaled image even outside of the view itself.
Here an example code I wrote. Try it out and you will see, that the card is clickable above itself more that just a few pixels. Take for testing any image you would like that is much higher than the available space like a portrait screenshot.
struct ContentView: View {
var exampleImageName: String = "testimage"
@State var clicked: Bool = false
@State var removeImageCode: Bool = false
var body: some View {
VStack (alignment: .leading){
ZStack{
VStack (alignment: .leading, spacing: 12){
if(!removeImageCode){
Image(exampleImageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxHeight: 125)
.clipped()
}
HStack (alignment: .top){
VStack(alignment: .leading,spacing: 4) {
Text("Click on card to change color")
.font(.headline)
.fontDesign(.rounded)
Text("With the image you can also click above the card...")
.font(.system(size: 12))
.fontWeight(.regular)
.fontDesign(.rounded)
}
Spacer()
}
.padding(.horizontal)
.padding(.vertical)
}
.background(clicked ? .green : .red)
.cornerRadius(10)
.shadow(radius: 2)
}
.onTapGesture {
clicked.toggle()
}
Toggle("Remove image code", isOn: $removeImageCode)
}
.padding(.horizontal)
}
}
I tried nearly everything. Extracting, adding and removing specific styles etc. I am sure the error lies here, because when i take an image that fits in the given area from the proportions the clickable area is the area of the view as it should.
Hope I explained it understandable, else do not hesiate to ask for more information.
答案1
得分: 0
- 你可以在图像上添加
.allowsHitTesting(false)
。这个图像仍然会响应点击,因为它包含在ZStack
中,而ZStack
上有onTapGesture
。
Image(exampleImageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxHeight: 125)
.clipped()
.allowsHitTesting(false) // <-- 这里
- 或者,你可以给
ZStack
添加.contentShape
:
ZStack {
// 省略部分
}
.contentShape(Rectangle()) // <-- 这里
.onTapGesture {
clicked.toggle()
}
英文:
I'm not able to say whether it's expected behavior or not, but I found a couple of workarounds.
- You can add
.allowsHitTesting(false)
to the image. The image is still receptive to taps because it is contained inside theZStack
, which has theonTapGesture
.
Image(exampleImageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxHeight: 125)
.clipped()
.allowsHitTesting(false) // <-- HERE
- Alternatively, add
.contentShape
to theZStack
:
ZStack{
// Omitted
}
.contentShape(Rectangle()) // <-- HERE
.onTapGesture {
clicked.toggle()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论