首页 > 解决方案 > 如何使用滑动手势在图像滑块内实现导航链接?

问题描述

浏览这里的帖子以创建图像滑块,我开始考虑如何使用滑动手势实现导航链接,(注意这不是关于滑动手势导航到新视图,而是就像您在图像中滑动一样轻按导航到一个视图。)我一直在实现和试用下面的代码,注意我调用的其余结构是从上面链接中的代码复制的,只是实现不同,我发现通过添加一个导航链接到图像它不再允许滑动手势,我将如何解决这个问题,以便我可以滑动和导航图像?

PS:我考虑过使用滑块解决方案并在那里导航,但填充和 UI 产生了一些奇怪的结果,所以我不同意这个选项。

struct carousel: View {
    @State var index = 0

    var images = ["Keith Haring Portfolio Photo", "Roy Lichtenstein Portfolio Photo", "Andy Warhol Portfolio Photo", "Jean-Michel Basquiat Portfolio Photo"]

    var body: some View {
        VStack(spacing: 20) {
            PagingView(index: $index.animation(), maxIndex: images.count - 1) {
                ForEach(self.images, id: \.self) { imageName in
                        Image(imageName)
                            .resizable()
                            .scaledToFill()
                }
            }
            .aspectRatio(contentMode: .fit)
            .clipShape(RoundedRectangle(cornerRadius: 10))
            .padding(.horizontal)

        }
    }
}

标签: swiftui

解决方案


推荐阅读