如何更改ObservedObject

时间:2020-04-09 15:51:23

标签: swift xcode swiftui

* DataStoreCode:

如何在getTrailer函数中将变量分配给参数? 单击并且链接不起作用时,无法将数据发送到参数

class DataStore: ObservableObject {
        @Published var movie: MovieRoot?
        @Published var trailer: TrailerRoot?
        @Published var url: String?

        init() {
            self.getMovies()
            self.getTrailers(url: url ?? "")
        }

        func getMovies() {
            JSONService().getMovies(url: "https://api.themoviedb.org/3/movie/upcoming?api_key=594b8eb4999a812345136ee3ed1ebdb&language=tr-TR&page=1", model: movie) { (result) in
                self.movie = result
            }
        }

        func getTrailers(url: String) { //This parameter.
            JSONService().getMovies(url: url, model: trailer) { (result) in
                self.trailer = result
            }
        }
    }

我的ContentView代码:

ForEach(store.movie?.results ?? []) { item in 
VStack()
.onTapGesture {
           self.show.toggle()
           self.store.url = "https://api.themoviedb.org/3/movie/\(item.id)/videos?api_key=594b8eb499123456ad5136ee3ed1ebdb&language=en-US"
          print(self.store.trailer?.results)
        }
    }

JSON服务

class JSONService {

    func getMovies<T: Decodable>(url: String, model: T, completion: @escaping (T) -> ()) {
        guard let url = URL(string: url) else { return }

        URLSession.shared.dataTask(with: url) { (data, response, error) in

            guard let data = data else { return }

            do {
                let items = try JSONDecoder().decode(T.self, from: data)
                DispatchQueue.main.async {
                    completion(items)
                }
            } catch let error {
                print(error)
            }
        }
        .resume()
    }
}

ContentView

struct ContentView: View {

    @State var show: Bool
    //@State var movie: MovieRoot
    @ObservedObject var store = DataStore()
    @State var selectedMovie: Movie
    @State var id: Int
    var imageURL = "https://image.tmdb.org/t/p/w185_and_h278_bestv2"
    //var trailerURL = "https://api.themoviedb.org/3/movie/338762/videos?api_key=594b8eb4999a8b44ad5136ee3ed1ebdb&language=en-US"
    var body: some View {

        ZStack {

            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(store.movie?.results ?? []) { item in
                        GeometryReader { geometry in
                            VStack {
                                //Image("film")
                                WebImage(url: URL(string: self.imageURL + item.poster_path))
                                    .resizable()
                                    .retryOnAppear(true)
                                    .onSuccess()
                                    .renderingMode(.original)
                                    .cornerRadius(30)
                                    .frame(width: 185, height: 278)
                                    .shadow(color: Color.black.opacity(0.4), radius: 10, x: 0, y: 10)
                                    .animation(.spring(response: 0.5, dampingFraction: 0.6, blendDuration: 0))

                                Text(item.title)
                                    .font(.system(size: 20, weight: .bold))

                                CircleView(firstColor: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), secondColor: #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1), width: 50, height: 50, percent: item.vote_average)
                            }
                            .rotation3DEffect(Angle(degrees: Double(geometry.frame(in: .global).minX - 30) / -20), axis: (x: 0, y: 10.0, z: 0))
                            .frame(width: UIScreen.main.bounds.width)
                            .onTapGesture {
                                self.show.toggle()
                Here->          self.store.url = "https://api.themoviedb.org/3/movie/\(item.id)/videos?api_key=594b8eb4999a8b44ad5136ee3ed1ebdb&language=en-US"
                                print(self.store.trailer?.results)
                            }
                            .offset(y: self.show ? .zero : -UIScreen.main.bounds.height)
                            .animation(.easeIn)
                        }
                        .frame(width: UIScreen.main.bounds.width)
                    }
                }
                .frame(maxHeight: .infinity)
            }

            DetailView(show: $show, movie: $selectedMovie)
        }
//        .onAppear {
//            JSONService().getMovie(url: "https://api.themoviedb.org/3/movie/upcoming?api_key=594b8eb4999a8b44ad5136ee3ed1ebdb&language=tr-TR&page=1") { (result) in
//                self.movie.results = result.results
//            }
//        }
    }
}

如何检查DataSource类中的url变量是否已更改。我将item.id扔到url变量中,但是它不起作用。

我将“内容视图”中的数据重定向到“详细信息视图”页面。

DetailView

struct DetailView: View {
    @ObservedObject var trailer = DataStore()

    @Binding var show: Bool
    @Binding var movie: Movie
    var body: some View {

        ScrollView(.vertical, showsIndicators: false) {
        VStack {
                Image(systemName: "multiply.circle")
                    .imageScale(.large)
                    .padding()
                    .onTapGesture {
                        self.show.toggle()
                }
                .onDisappear()

                WebView(request: URLRequest(url: URL(string: "https://www.youtube.com/watch?v=gn5QmllRCn4")!))
                    .frame(height: UIScreen.main.bounds.height * 0.3)
                    .background(Color(UIColor.secondarySystemBackground))
                    .cornerRadius(20)
                    .shadow(color: Color.black.opacity(0.3), radius: 10, x: 0, y: 10)

                HStack {
                    Text(movie.title)
                        .font(.system(size: 20, weight: .semibold))
                    Spacer()
                    CircleView(firstColor: #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1), secondColor: #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1), width: 30, height: 30, percent: 4.6)
                }
                .padding(.horizontal)
                .background(Color(UIColor.secondarySystemBackground))
                .cornerRadius(10)
                .shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: 5)
                .padding()

            Text(movie.overview)
                    .padding()
                    .background(Color(UIColor.secondarySystemBackground))
                    .cornerRadius(20)
                    .padding(.horizontal)
                    .shadow(color: Color.black.opacity(0.3), radius: 10, x: 0, y: 10)
            }
        }
        .animation(.easeIn)
        .offset(y: show ? UIScreen.main.bounds.height : .zero)
        .onReceive(trailer.$url) { (_) in

        }
    }
}

0 个答案:

没有答案