有没有办法通过 .onLongPressGesture 将第三个切换选项添加到开/关状态? [英] Is there a way to add a third toggle option to an on/off state by .onLongPressGesture?

查看:13
本文介绍了有没有办法通过 .onLongPressGesture 将第三个切换选项添加到开/关状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了如下图所示的切换开关来打开/关闭图像或通过/失败.我正在尝试使用长按手势向图像添加第三个状态,这将使用斜线图标将图像变成灰色.我已经在文本元素中实现了这一点,因为它没有 bool 条件,但经过多次搜索后无法弄清楚如何通过图像实现这一点.

I already have a toggle set up as in the image below to toggle the image on/off or pass / fail. I am trying to add a third state using a long press gesture to the image which will turn the image grey with a slash icon. I have achieved this in the text element as at is doesn't have an bool condition, but after much searching can't figure out how to achieve this with the image.

我的目标是:

Image(systemName: isPressed ? "checkmark.circle.fill" : "x.circle.fill" || isLongPressed ? "checkmark.circle.fill" : "slash.circle.fill")

虽然我知道这是不正确的.

Though I know this is not correct.

struct element: View {
    var section: CleaningElements = courseSections[0]
    
    @State private var isPressed = true
    @State private var isLongPressed = true
    
    var body: some View {
        HStack(alignment: .top) {
            Button(action: {
                    self.isPressed.toggle() }) {
                Image(systemName: isPressed ? "checkmark.circle.fill" : "x.circle.fill")
                    .font(.system(size: 20))
                    .foregroundColor(isPressed ? .green : .red)
                    .frame(width: 36, height: 36)
                    .background(RoundedRectangle(cornerRadius: 12, style: .continuous).fill(section.color))
            }
            Text(section.title)
                .font(.subheadline)
                .bold()
                .opacity(isLongPressed ? 1.0 : 0.3)
                .foregroundColor(isLongPressed ? Color.primary : Color.secondary)
                .onLongPressGesture {
                    self.isLongPressed.toggle()
                }
            Spacer()
        }
        .padding(3)
    }
}

推荐答案

这是一个可能的解决方案的演示(带有简化的代码) - 保留用于突出显示的按钮,但动作处理但自定义手势.使用 Xcode 12.1/iOS 14.1 测试

Here is a demo (with simplified your code) of possible solution - keep button for highlight but actions handle but custom gestures. Tested with Xcode 12.1 / iOS 14.1

struct element: View {
    
    @State private var isPressed = false
    @State private var isLongPressed = false
    
    var body: some View {
        Button(action: {}) {
            Group {
                if isLongPressed {
                    Image(systemName: "minus.circle")
                        .foregroundColor(.gray)
                } else {
                    Image(systemName: isPressed ? "checkmark.circle.fill" : "x.circle.fill")
                        .foregroundColor(isPressed ? .green : .red)
                }
            }
            .font(.system(size: 20))
            .frame(width: 36, height: 36)
            .background(RoundedRectangle(cornerRadius: 12, style: .continuous).fill(Color.yellow))
            .gesture(TapGesture().onEnded {
                isLongPressed = false
                self.isPressed.toggle()
            })
            .gesture(LongPressGesture().onEnded { _ in
                isLongPressed = true
            })
        }
    }
}

这篇关于有没有办法通过 .onLongPressGesture 将第三个切换选项添加到开/关状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆