使用.onTapGesture

时间:2020-10-29 18:43:42

标签: swift swiftui

我正在开发应用程序。这会创建一个堆叠式旋转木马滑块。滑块中的项目从确认Identifiable协议的数组中排成一行。使用的资产在xcassets文件夹中。该应用程序主要包括三个基本部分:

a)Home()。这是主要的轮播滑块视图。
b)在Home()上方有一个与ZStack分层的“阅读更多”按钮(请参阅:所附图像)
c)在“详细信息屏幕”中,我创建了一些粗略的视图。基本代码正常工作后,我将详细实现此屏幕。

我在b)中面临挑战。那就是我无法将“ main”封闭变量传递给.onTapGesture的封闭。因此,我的“阅读更多”按钮不起作用。我猜,这是因为我正在接近数组。我仅将下面的相关代码与屏幕截图粘贴在一起,以提供更好的参考。任何帮助将不胜感激。

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        Home()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct Home: View {
    
    var width = UIScreen.main.bounds.width - (40 + 60)
    var height = UIScreen.main.bounds.height/1.15
    @State var show = false
    @State var books = [
        
        // ensuring id in ascending Order....
        
        Book(id: 0, image: "p1", offset: 0),
        Book(id: 1, image: "p0", offset: 0),
        Book(id: 2, image: "p3", offset: 0),
        Book(id: 3, image: "p2", offset: 0),
        Book(id: 4, image: "p5", offset: 0),
        Book(id: 5, image: "p4", offset: 0),
    ]
    
    @State var swiped = 0
    @State var selectedIndex = 0
    @Namespace var name
    
    var body: some View{
        
        VStack{
            
           ZStack {
                
             ForEach(books.reversed()) { book in
                    ZStack {
                        Image(book.image)
                          .resizable()
                          .aspectRatio(contentMode: .fill)
                          .frame(width: width, height:getheight(index: book.id))  //  .frame(width: width, height: height)
                          .cornerRadius(25)
                          .shadow(color: Color.blue.opacity(0.5), radius: 5, x: 5)
                    
                        CardView(card: book)
                            .frame(width: width, height: getheight(index: book.id))
                            .onTapGesture{
                                withAnimation(.spring()){
                                 selectedIndex = book
                                    //error: Cannot assign value of type 'ReversedCollection<[Book]>.Element' (aka 'Book') to type 'Int'
                                    show.toggle()
                                }
                            }
                    }
                  .offset(x: book.id - swiped < 3 ? CGFloat(book.id - swiped) * 30 : 60)
                  //  print(swiped)
                    .padding(.vertical, 20)
                    .padding(.horizontal,40)
           //     }
                 .contentShape(Rectangle())
                 .padding(.horizontal,20)
                // .offset(x: (book.offset - 35))
                 .offset(x: book.offset)
                    .gesture(DragGesture().onChanged({ (value) in
                        withAnimation{onScroll(value: value.translation.width, index: book.id)}
                    }).onEnded({ (value) in
                        withAnimation{onEnd(value: value.translation.width, index: book.id)}
                    }))
                    
              }
          }.frame(height: height)
           .padding(.vertical, 50)
           .padding(.horizontal, 20)
            
            if show {
                VStack {
                    VStack {
                        HStack{
                            Button(action:{}) {
                                Image(systemName: "suit.heart")
                                    .font(.system(size: 22))
                                    .foregroundColor(.white)
                            }
                            Spacer()
                            
                            Button(action:{
                                
                                //closing hero view...
                                
                                withAnimation(.spring()){
                                 
                                    
                                    show.toggle()
                                }
                                
                                
                            }) {
                                
                                Image(systemName: "xmark")
                                    .font(.system(size: 22))
                                    .foregroundColor(.white)
                            }
                        }
                        Image("p\(selectedIndex)")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(height: 250)
                            .matchedGeometryEffect(id: "p\(selectedIndex)", in: name)
                            .rotationEffect(.init(degrees: 12))
                    }
                    
                }
              
                .background(Color.white)
            }
        }  .background(Color("bg").ignoresSafeArea(.all, edges: .all)) //
}

enter image description here

0 个答案:

没有答案