提问人:alex king 提问时间:7/13/2023 最后编辑:alex king 更新时间:7/13/2023 访问量:204
适用于 IOS 的 swiftUI 悬停
swiftUI hover for IOS
问:
我尝试制作像swiftUI菜单一样的悬停效果(滑动按钮时背景颜色会发生变化,例如:veed.io/view/85e8df0d-14b6-44d1-a88e-31ac663246d1)
Menu("Image Editing") {
Button {} label: {
Label("Markup", systemImage: "pencil.tip.crop.circle")
}
Button {} label: {
Label("Crop", systemImage: "selection.pin.in.out")
}
Button {} label: {
Label("Compress", systemImage: "rectangle.compress.vertical")
}
}
对于macOS,我使用了悬停属性
struct MenuButtonView: View {
@State private var isHovered = false
var item: UIMenuElement
var body: some View {
Button {
print("\(item.title) tapped")
} label: {
HStack {
Text(item.title)
}
}
.buttonStyle(MenuButtonStyle(isHovered: $isHovered))
.onHover { hovering in
isHovered = hovering
}
.frame(height: 20)
}
}
struct MenuButtonStyle: ButtonStyle {
@Binding var isHovered: Bool
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(.white)
.background(
Rectangle()
.fill(
configuration.isPressed
? Color.gray.opacity(0.15)
: isHovered ? Color.gray.opacity(0.15) : Color.clear
)
.frame(height: 40)
)
.scaleEffect(configuration.isPressed ? 0.95 : 1.0)
}
}
它工作正常,但对于 iOS,我无法获得这种效果,我尝试了这样的东西
.gesture(
LongPressGesture(minimumDuration: 0)
.updating($isLongPressed) { value, state, _ in
state = value
isHovered = value
}
)
or
DragGesture(minimumDistance: 0)
.updating($isHovered) { value, state, _ in
state = value.startLocation == value.location
isHovered = value.startLocation == value.location
}
但是没有效果。你能告诉我如何制作这个效果吗?或者也许有可能以程序化方式显示?Menu
答: 暂无答案
上一个:如何捕获所有iOS控制台日志?
评论