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