SwiftUI: перейдите к другому представлению с помощью .onTapGuesture {} - PullRequest
0 голосов
/ 12 июля 2020

Для следующего CardView() я хочу, чтобы пользователь нажал на него, чтобы перейти к WebView() со строкой ссылки в post.article_url.

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
            }
        )}}

Я пытался обернуть CardView() с NavigationLink(). Навигация прошла успешно, но изображение и текст на CardView() стали полностью синими. Может быть, это можно решить с помощью других средств (например, с помощью некоторых кодов в .onTapGuesture{})?

Кроме того, поскольку это стопка карт, которую предполагается удалить. NavigationLink() также блокирует жест перетаскивания на этих картах.

Заранее спасибо!

1 Ответ

1 голос
/ 12 июля 2020

Введите переменную (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() }

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...