Я создаю что-то похожее на историю в Instagram. Но что бы я ни пробовал, либо тег tabview, либо жест касания, работает. Я не мог заставить их обоих работать вместе. Цель состоит в том, что если я коснусь левой или правой стороны представления, он перейдет к другой истории той же модели истории. если я прокрутлю TabView, он перейдет к другой StoryModel.
Это расширение, которое я пробовал
struct OnTap: ViewModifier {
let response: (CGPoint) -> Void
@State private var location: CGPoint = .zero
func body(content: Content) -> some View {
content
.onTapGesture {
response(location)
}
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onEnded { location = $0.location }
)
}
}
extension View {
func onTapGesture(_ handler: @escaping (CGPoint) -> Void) -> some View {
self.modifier(OnTap(response: handler))
}
}
struct OnTapAndTagAction: ViewModifier {
let onTap: (CGPoint) -> Void
let index: Int
func body(content: Content) -> some View {
content
.overlay(
Rectangle()
.fill(Color.clear)
.onTapGesture { tapLocation in
onTap(tapLocation)
}
)
.tag(index)
}
}
extension View {
func onTapGestureAndTag(index: Int, _ handler: @escaping (CGPoint) -> Void) -> some View {
self.modifier(OnTapAndTagAction(onTap: handler, index: index))
}
}
И вот как я его использую
struct StoryDetailView: View {
ZStack {
Color.storyNotchPurple
.edgesIgnoringSafeArea(.top)
TabView(selection: $selectedStoryId) {
Color.storyNotchPurple.edgesIgnoringSafeArea(.top)
ForEach(storyModelArray.indices, id: \.self) { index in
GeometryReader {...}
.onTapGestureAndTag(index: index) { tapLocation in
if tapLocation.x < screenWidth / 2 {
if currentStoryIndex > 1 {
currentStoryIndex -= 1
}
} else {
if currentStoryIndex < progressCount {
currentStoryIndex += 1
}
}
}
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.edgesIgnoringSafeArea(.top)
}
Да, проект iOS 14.
вместо жеста касания я использовал кнопки и индекс z
HStack {
Button(action: {
if currentStoryIndex > 1 {
currentStoryIndex -= 1
}
}) {
Color.clear
.contentShape(Rectangle())
.frame(width: screenWidth / 2, height: g.size.height - 100)
}
.buttonStyle(PlainButtonStyle())
Button(action: {
if currentStoryIndex < progressCount {
currentStoryIndex += 1
}
}) {
Color.clear
.contentShape(Rectangle())
.frame(width: screenWidth / 2, height: g.size.height - 100)
}
.buttonStyle(PlainButtonStyle())
}
.zIndex(1)
Почему вы заново изобретаете onTapGesture? Поддерживаете ли вы версию ниже 17.0?