SwiftUI:使用.onTapGuesture{}导航到另一个视图



对于下面的CardView(),我希望用户点击它,使用post.article_url中的链接字符串导航到WebView()

ForEach(self.posts.reversed(), id: .id) {post in
GeometryReader{geo -> AnyView in
return AnyView(
CardView (index: self.getPostIndex(post: post),
title: post.title,
image: post.cover_image,
author: post.author, 
source: post.source, description: post.description,
imageWidth: self.cardWidth, 
imageHeight: self.imageHeight(index: self.getPostIndex(post: post))
)
.animation(.spring())
.frame(width: self.widthAmount(index: self.getPostIndex(post: post)), 
height: self.heightAmount(index: self.getPostIndex(post: post)))
.offset(x: self.CardOffsetAmountHorizontal(index: self.getPostIndex(post: post)),
y: self.CardOffsetAmountVertical(index: self.getPostIndex(post: post)))
.onTapGesture{
// navigate to a webview with url: post.article_url
}
)}}

我试着用NavigationLink()包装CardView()。导航是成功的,但它将CardView()上的图像和文本变成了完全的蓝色。也许这可以通过其他方式解决(比如.onTapGuesture{}中的一些代码(?

此外,因为这是一个应该被刷的卡叠。NavigationLink()还阻止这些卡上的拖动手势。

提前感谢!

引入一个变量(Bool(,用空的NavigationLink监视该变量。当捕获选项卡时,切换此变量,然后将激活重定向

// This variable 
@State private var goToNewView: Bool = false
ForEach(self.posts.reversed(), id: .id) {post in
GeometryReader{geo -> AnyView in
return AnyView(
CardView (index: self.getPostIndex(post: post),
title: post.title,
image: post.cover_image,
author: post.author, 
source: post.source, description: post.description,
imageWidth: self.cardWidth, 
imageHeight: self.imageHeight(index: self.getPostIndex(post: post))
)
.animation(.spring())
.frame(width: self.widthAmount(index: self.getPostIndex(post: post)), 
height: self.heightAmount(index: self.getPostIndex(post: post)))
.offset(x: self.CardOffsetAmountHorizontal(index: self.getPostIndex(post: post)),
y: self.CardOffsetAmountVertical(index: self.getPostIndex(post: post)))
.onTapGesture{
// navigate to a webview with url: post.article_url
// Toggle the variable here
self.goToNewView.toggle()
}
)}}

// Put this in your view. It will not be visible
NavigationLink(destination: MyOtherView(), isActive: self.$goToNewView) { EmptyView() }

相关内容

  • 没有找到相关文章

最新更新