英文:
Tap gestures don't recognise offset change on a View in SwiftUI
问题
I have a "button" that I need to offset to a different position using the .offset(y: someVal)
.
I have the layout of the View as I like it but the hit area seems to be at the area before the offset was applied.
This is my code:
var body: some View {
ZStack {
ZStack {
Circle()
.foregroundColor(.white)
Image(systemName: "someIcon")
.foregroundColor(myColor)
}
.frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
.overlay(
RoundedRectangle(cornerRadius: centerBtnRadius)
.stroke(myColor, lineWidth: 1)
)
.onTapGesture {
//...
}
.onLongPressGesture(perform: {
//...
})
.offset(y: centerBtnOffsetY)
VStack {
Spacer()
Text(labelText)
.scaledToFit()
}
}
.frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
}
Now I've tried placing the modifier before others on the nested ZStack
without success. It also must be after the .overlay
modifier that positions my border around the Circle()
. (I'm also open for ideas to make border different if it needs to be a part of the solution!)
My question is:
How can I make sure that my press gestures are caught by the whole Circle element after I've re-positioned it with the .offset
modifier? 🤔
英文:
I have a "button" that I need to offset to a different position using the .offset(y: someVal)
.
I have the layout of the View as I like it but the hit area seems to be at the area before the offset was applied.
This is my code:
var body: some View {
ZStack {
ZStack {
Circle()
.foregroundColor(.white)
Image(systemName: "someIcon")
.foregroundColor(myColor)
}
.frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
.overlay(
RoundedRectangle(cornerRadius: centerBtnRadius)
.stroke(myColor, lineWidth: 1)
)
.onTapGesture {
//...
}
.onLongPressGesture(perform: {
//...
})
.offset(y: centerBtnOffsetY)
VStack {
Spacer()
Text(labelText)
.scaledToFit()
}
}
.frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
}
Now I've tried placing the modifier before others on the nested ZStack
without success. It also must be after the .overlay
modifier that positions my border around the Circle()
. (I'm also open for ideas to make border different if it needs to be a part of the solution!)
My question is:
How can I make sure that my press gestures are catched by the whole Circle element after I've re-positioned it with the .offset
modifier? 🙏
答案1
得分: 1
将偏移修饰符从点击手势修饰符之后移到之前。通过这样做,点击手势修饰符将考虑偏移量以确定“点击区域”。
var body: some View {
ZStack {
ZStack {
Circle()
.foregroundColor(.white)
Image(systemName: "someIcon")
.foregroundColor(myColor)
}
.frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
.overlay(
RoundedRectangle(cornerRadius: centerBtnRadius)
.stroke(myColor, lineWidth: 1)
)
.onTapGesture {
//...
}
.onLongPressGesture(perform: {
//...
})
.offset(y: centerBtnOffsetY) // 已更改的行
VStack {
Spacer()
Text(labelText)
.scaledToFit()
}
}
.frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
}
英文:
You need to move the offset modifier from after the tap gestures modifiers to before. By doing this, the tap gestures modifiers will take the offset into account for the "tap area".
var body: some View {
ZStack {
ZStack {
Circle()
.foregroundColor(.white)
Image(systemName: "someIcon")
.foregroundColor(myColor)
}
.frame(width: centerBtnRadius * 2, height: centerBtnRadius * 2)
.overlay(
RoundedRectangle(cornerRadius: centerBtnRadius)
.stroke(myColor, lineWidth: 1)
)
.offset(y: centerBtnOffsetY) // Changed line
.onTapGesture {
//...
}
.onLongPressGesture(perform: {
//...
})
VStack {
Spacer()
Text(labelText)
.scaledToFit()
}
}
.frame(width: Constants.tbItemWidth, height: Constants.tbItemHeight)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论