如何在SwiftUI中设计视图并为服务器图像提供旋转效果?

huangapple go评论53阅读模式
英文:

How to design the view and give rotationEffect for server images in swiftui?

问题

以下是您要翻译的内容:

This is what I want this view design for static images:

如何在SwiftUI中设计视图并为服务器图像提供旋转效果?

I want to design a view which have rotation effect each view have different degree rotation from

[-10,10,0] this array and set to the images. images comes from Api.

this is my Approach

    @State var rotate: [Int] = [-10, 10, 0]
    @State var index: Int = -1
    @StateObject var list = StacksViewModel()
    var body: some View{
        VStack{
        ForEach(list.datas, id: \.id){ item in
               ZStack{
                ForEach(item.stack_book_link, id: \.id){ images in
                    
                        AsyncImage(url: URL(string: images.book_url))
.frame(width: 125, height: 135)
.rotationEffect(Angle(degrees: Double(rotate[ self.index + 1])))  
                    }
                }
                
            }
        }
        .onAppear{
            list.getStacksData()
        }
    }
}
英文:

This is what I want this view design for static images:

如何在SwiftUI中设计视图并为服务器图像提供旋转效果?

I want to design a view which have rotation effect each view have different degree rotation from

[-10,10,0]

this array and set to the images. images comes from Api.

this is my Approach

struct Stacks: View{
    @State var rotate: [Int] = [-10, 10, 0]
    @State var index: Int = -1
    @StateObject var list = StacksViewModel()
    var body: some View{
        VStack{
        ForEach(list.datas, id: \.id){ item in
               ZStack{
                ForEach(item.stack_book_link, id: \.id){ images in
                    
                        AsyncImage(url: URL(string: images.book_url))
.frame(width: 125, height: 135)
.rotationEffect(Angle(degrees: Double(rotate[ self.index + 1])))  
                    }
                }
                
            }
        }
        .onAppear{
            list.getStacksData()
        }
    }
}

答案1

得分: 0

通过迭代 item.stackBookLink.indices,你可以这样做:

struct ContentView: View {
    
    @State var rotate: [Int] = [-10, 10, 0]
    @State var index: Int = -1
    @StateObject var list = StacksViewModel()
    
    var body: some View{
        VStack{
            ForEach(list.datas, id: \.id) { item in                
                ZStack{                
                    ForEach(item.stackBookLink.indices, id: \.self) { index in
                        AsyncImage(url: URL(string: item.stackBookLink[index].bookUrl))
                            .frame(width: 125, height: 135)
                            .rotationEffect(Angle(degrees: Double(rotate[index]))
                            .shadow(radius: 5)
                    }
                }                
            }
        }
        .onAppear{
            list.getStacksData()
        }
    }
}


struct BookLink: Identifiable {
    let id = UUID()
    let bookUrl: String
}

struct Item {
    let id = UUID()
    let stackBookLink: [BookLink]
}

class StacksViewModel: ObservableObject {
    
    @Published var datas: [Item] = []
    
    func getStacksData() {
        datas = [Item(stackBookLink: [BookLink(bookUrl: "https://picsum.photos/200/300"),
                                      BookLink(bookUrl: "https://picsum.photos/200/300"),
                                      BookLink(bookUrl: "https://picsum.photos/200/300")])]
    }
}

此外,请注意,Swift 中的变量名使用 lowerCamelCase,而不是 snake_case

英文:

Iterating through item.stackBookLink.indices you can do it like this:

struct ContentView: View {
    
    @State var rotate: [Int] = [-10, 10, 0]
    @State var index: Int = -1
    @StateObject var list = StacksViewModel()
    
    var body: some View{
        VStack{
            ForEach(list.datas, id: \.id) { item in                
                ZStack{                
                    ForEach(item.stackBookLink.indices, id: \.self) { index in
                        AsyncImage(url: URL(string: item.stackBookLink[index].bookUrl))
                        
                            .frame(width: 125, height: 135)
                            .rotationEffect(Angle(degrees: Double(rotate[ index])))
                            .shadow(radius: 5)
                    }
                }                
            }
        }
        .onAppear{
            list.getStacksData()
        }
    }
}


struct BookLink: Identifiable {
    let id = UUID()
    let bookUrl: String
}

struct Item {
    let id = UUID()
    let stackBookLink: [BookLink]
}

class StacksViewModel: ObservableObject {
    
    
    @Published var datas: [Item] = []
    
    func getStacksData() {
        datas = [Item(stackBookLink: [BookLink(bookUrl: "https://picsum.photos/200/300"),
                                          BookLink(bookUrl: "https://picsum.photos/200/300"),
                                          BookLink(bookUrl: "https://picsum.photos/200/300")])]
    }
}

如何在SwiftUI中设计视图并为服务器图像提供旋转效果?

Also, note that variable names in Swift are written using lowerCamelCase, not snake_case

答案2

得分: 0

尝试这种方法,这是您想要显示的主视图。

struct Stacks: View {
    @State var rotate: [Int] = [-20, 20, 0]
    @State var index: Int = -1
    @StateObject var list = StacksViewModel()
    
    var body: some View {
        VStack(spacing: 35) {
            ForEach(list.datas, id: \.id) { item in
                ZStack {
                    ForEach(item.stack_book_link.indices, id: \.self) { index in
                        AsyncImage(url: URL(string: item.stack_book_link[index].book_url)) { img in
                            img.resizable().frame(width: 125, height: 155)
                                .rotationEffect(Angle(degrees: Double(rotate[index])))
                                .shadow(radius: 5)
                        } placeholder: {
                            Image("u").resizable().frame(width: 125, height: 155).rotationEffect(Angle(degrees: Double(rotate[index])))
                                .shadow(radius: 5)
                        }
                    }
                }
            }
        }
        .onAppear {
            list.getStacksData()
        }
    }
}

这是ViewModel。

class StacksViewModel: ObservableObject {
    @Published var datas = [StacksDatum]()
    @Published var image: [String] = []
    
    func getStacksData() {
        let defaults = UserDefaults.standard
        guard let token = defaults.string(forKey: "access_token") else {
            return
        }
        //进行身份验证(StacksService())然后获取数据
        StacksService().getStacksData(token: token) { (result) in
            switch result {
                case .success(let results):
                    DispatchQueue.main.async {
                        self.datas = results.studentStacks.data
                        
                        for data in self.datas {
                            for imgdata in data.stack_book_link {
                                self.image.append(imgdata.book_url)
                            }
                            print(self.datas)
                        }
                    }
                case .failure(let error):
                    print(error.localizedDescription)
            }
        }
    }
}

在我的端侧显示得非常正确。我的系统是Xcode 14.1和iOS 15。StacksService()是一个类,用于验证用户是否通过身份验证。

英文:

> Try this approach and this is main View which you want to show.

struct Stacks: View{
     @State var rotate: [Int] = [-20, 20, 0]
           @State var index: Int = -1
           @StateObject var list = StacksViewModel()
           
           var body: some View{
               VStack(spacing: 35){
                   ForEach(list.datas, id: \.id) { item in
                       ZStack{
                           ForEach(item.stack_book_link.indices, id: \.self) { index in
                               AsyncImage(url: URL(string: item.stack_book_link[index].book_url)){ img in
                                   img.resizable().frame(width: 125, height: 155)
                                       .rotationEffect(Angle(degrees: Double(rotate[ index])))
                                       .shadow(radius: 5)
                               }placeholder: {
                                   Image("u").resizable().frame(width: 125, height: 155).rotationEffect(Angle(degrees: Double(rotate[ index])))
                                       .shadow(radius: 5)
                               }
    
                           }
                       }
                   }
               }
               .onAppear{
                   list.getStacksData()
               }
           } 
    }

> This is ViewModel.

class StacksViewModel: ObservableObject{
   
    @Published var datas = [StacksDatum]()
    @Published var image: [String] = []
   
    func getStacksData() {
        
        let defaults = UserDefaults.standard
        guard let token = defaults.string(forKey: "access_token") else {
            return
        }
        //Give authentication(StacksService()) and then fetching data 
        StacksService().getStacksData(token: token){ (result) in
            switch result {
                case .success(let results):
                    DispatchQueue.main.async {
                        self.datas = results.studentStacks.data
                        
                        for data in self.datas {
                            for imgdata in data.stack_book_link{
                                self.image.append(imgdata.book_url)
                            }
                            
                        }
                        print(self.datas)
                    }
                case .failure(let error):
                    print(error.localizedDescription)
            }
        }
    }
      
}

> In my side it shows very correctly. my system xcode 14.1 and ios 15. StacksService() is a class and it gives authentication that the user is authenticate or not.

huangapple
  • 本文由 发表于 2023年2月8日 17:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定