适用于 IOS 的 swiftUI 悬停

swiftUI hover for IOS

提问人:alex king 提问时间:7/13/2023 最后编辑:alex king 更新时间:7/13/2023 访问量:204

问:

我尝试制作像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

迅捷 SwiftUI

评论

1赞 timbre timbre 7/13/2023
AFAIK onHover 仅在您有“指针”(例如鼠标)时才有效。您是否使用插入鼠标/其他指针设备进行了测试?
0赞 alex king 7/13/2023
@timbretimbre onHover 适用于 macOS,但我还需要添加对 iOS 的支持。我知道悬停它不支持 iOS,但我们可以做同样的事情,不是吗?例如,swiftUI 中的 Menu。问题是idk如何,我尝试了LongPressGesture和DragGesture,但没有效果
0赞 lorem ipsum 7/13/2023
在iOS中没有悬停这样的事情。你必须重新思考你想做什么。例如,突出显示屏幕中央的内容或使用点击和双击等。
0赞 alex king 7/13/2023
@loremipsum我尝试制作类似 veed.io/view/85e8df0d-14b6-44d1-a88e-31ac663246d1 它是在对焦时突出显示按钮的东西。对于 mac,我是否使用“onHover”并更新背景 - 它工作正常,但对于 iOS,我不知道该用什么来产生这种效果。你能帮我吗?
0赞 lorem ipsum 7/14/2023
这在 iOS 中是不存在的。没有办法复制它,因为没有这样的操作。

答: 暂无答案