UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

LongTapGesture Overwrites existing swipe gesture of TabView in PageTabViewStyle

Forums > SwiftUI

Hey,

I'm trying to create a multiple image picker, and I am using a Tab View in PageTabViewStyle to display the image such that the user can swipe through them. With the LongTapGesture I wanted to implement the option that the a button on the top right shows, which the user can tap to delete the current image that is being viewed. However, when add the .onLongTapGesture view modifier, the swipe gesture of the TabView doesn't work anymore.

This is the code:

NavigationView {
            Form {
                Button(action: {
                    if photos.isEmpty {
                        self.showPicker.toggle()
                    }
                }) {
                    if photos.isEmpty {
                        HStack{
                            Spacer()
                            Image(systemName: "photo")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 100, height: 100)
                            Spacer()
                        }
                    } else {
                        TabView(selection: $pageSelection){
                            ForEach(photos.indices) { photo in
                                    Image(uiImage: photos[photo])
                                        .resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .frame(minWidth: 0, maxWidth: .infinity)
                                        .frame(height: 300)
                                        .tag(photo)
                                        .onLongPressGesture {
                                            self.press.toggle()
                                        }
                            }
                        }
                        .tabViewStyle(PageTabViewStyle())
                        .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
                    }
                }.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
                Rectangle()
                    .frame(width: 100, height: 100)
                    .foregroundColor(press ? .black : .green)
            }
            .navigationTitle("Multiple Photos")
        }
        .sheet(isPresented: self.$showPicker) {
            MultiplePhotosPicker(photos: self.$photos, showPicker: $showPicker)
        }

Note that currently the delete button is not implemented rather I used a rectangle to check whether it recognises the longtapgesture. With this code the swipe gesture of the tabview does not work. However, if I add another onTapGesture modifier right before the onLongPressGesture modifer, that increases the current index by 1, the swipe gesture magically starts to work again; But with it comes that the pagealso changes on a short tap, which I do not want. Here is what I mean for reference:

Image(uiImage: photos[photo])
                                        .resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .frame(minWidth: 0, maxWidth: .infinity)
                                        .frame(height: 300)
                                        .tag(photo)
                                        **.onTapGesture {
                                            self.pageSelection = photo +1
                                        }**
                                        .onLongPressGesture {
                                            self.press.toggle()
                                        }

Although, this works, I have no idea why the swipe gesture suddenly is recognised again. And as I said I do not actually want to use the .onTapGesture here, I simply discovered that the swipe gesture works again when using this as I was trying to find a solution . So my question is: Is there way to implement a LongPressGesture with a TabView in PageTabViewStyle() without it overriding/disabling the already existing swipe gesture of the tabview?

Thank You In Advance, Ben

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.