skip to Main Content

I am new to swift, so thanks for all the help… I am working on my first app and this works great if my button is press it goes into the didTap function.. But I need to do something else if its a LONG HOLD on the button…

Being new to this I need some help please… Here is more of the code so you can understand my coding, style,

As of right now NONE of the examples worked

var body: some View {
        ZStack(alignment: .bottom) {
            
            Color.black.ignoresSafeArea()
            
            VStack {
                HStack {
                    Spacer()
                    Text(currentValue)
                        .foregroundColor(.white)
                        .font(.system(size: 72))
                }
                .padding()
                
                ForEach(buttons, id: .self) { row in
                    HStack {
                        ForEach(row, id: .self) { button in
                            Button {
                                buttonTapped(button: button)
                            } label: {
                                HStack {
                                    Text(button.rawValue)
                                        .foregroundColor(button.foregroundColor)
                                        .font(.system(size: 30, weight: .semibold))
                                        .frame(width: button.getButtonWidth(), height: button.getButtonWidth())
                                    
                                    if(button.isZeroButton) {
                                        Spacer()
                                    }
                                }
                                .frame(width: button.width, height: button.height)
                                .background(button.backgroundColor)
                                .cornerRadius(button.height)
                            }
                        }
                    }
                }
            }
            .padding(.bottom)
        }
    }
    

2

Answers


  1. We use onTapGesture for tap actions, and onLongPressGesture for long press actions:

    Button(action: {}, label: {
       Text(item.rawValue)
           .font(.system(size: 32))
           .frame(width: self.buttonWidth(item: item), height: self.buttonHeight())
           .background(item.buttonColor)
           .foregroundColor(.white)
           .cornerRadius(self.buttonWidth(item: item)/2)
           .onTapGesture { //action for Tap }
           .onLongPressGesture(minimumDuration: 0.5) {
            action()
        }
    })
    
    Login or Signup to reply.
  2. you could just use Text instead of a Button, such as:

        ForEach(row, id: .self) { item in
            Text(item.rawValue)
                .font(.system(size: 32))
                .frame(width: self.buttonWidth(item: item),height: self.buttonHeight())
                .background(item.buttonColor)
                .foregroundColor(.white)
                .cornerRadius(self.buttonWidth(item: item)/2)
                .onTapGesture {
                    // put your button action here
                    self.didTap(button: item)
                    print("---> onTapGesture")
                }
                .onLongPressGesture {
                    print("---> onLongPressGesture")
                }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search