如何解决只显示第一张图片,后续图片消失
我目前正在使用 const formFields = this.memberService.aryFormFields;
let aryErrors: Array<{[key : string] : any}> = [];
for(var i =0; i < formFields.length; i++){
//loop tru the empty aryErrors
Object.keys(formFields[i]).forEach(key=> {
var objErrors = formControls[key].errors;
var formObj = key;
//if has error
if (objErrors) {
//loop tru the errors
Object.keys(objErrors).forEach(key => {
//retrive the relevant message from ValidationFormsService
if (this.vf.errorMessages.hasOwnProperty(formObj)) {
aryErrors[i]={ [formObj] : this.vf.errorMessages[formObj][key]};
}
else {
//if not exist,using formObj
aryErrors[i]={ [formObj] : this.toCamelCase(formObj) + " is "+ key +"!"};
}
});
}
});
}//for
return aryErrors;
开发应用程序并尝试制作平移手势视图。
我使用 SwiftUI
以平移手势显示多个图像。
当我打开 Kingfisher
视图时,会显示多个卡片视图中的第一张图像,但我滑动时不显示后续图像。显示的第一张图片也消失了。
我该如何解决这个问题?
代码如下:
PanGesture.swift
Hstack
viewmodel.swift
import SwiftUI
import Kingfisher
import FirebaseStorage
struct PanListView: View {
@Observedobject var viewmodel:viewmodel
var body: some View {
NavigationView{
vstack{
List(self.viewmodel.posts.indices,id: \.self){index in
ZStack{
Button(action: {
viewmodel.selectedDetailIndex = Double(index)
let postCountDevided2:Double = Double(viewmodel.posts.count) / 2
viewmodel.initialIndex = (postCountDevided2 - 0.5) - viewmodel.selectedDetailIndex
self.viewmodel.initialX = CGFloat((Double(UIScreen.main.bounds.width) - 15) * self.viewmodel.initialIndex)
self.viewmodel.x = self.viewmodel.initialX
if ((viewmodel.posts.count % 2) == 0){
self.viewmodel.swipePostsCount = CGFloat(viewmodel.initialIndex + 0.5)
}else{
self.viewmodel.swipePostsCount = CGFloat(viewmodel.initialIndex)
}
self.viewmodel.detailShowArray[Int(viewmodel.selectedDetailIndex)] = true
},label: {
Text(self.viewmodel.posts[index].word)
})
NavigationLink(
destination: Panswipeview(
viewmodel: viewmodel,posts: self.viewmodel.posts
)
){
EmptyView()
}.frame(width:0).opacity(0)
}
}
}
.navigationBarTitle("ListView",displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
.onAppear(){
self.viewmodel.getAllPosts(){(status) in
for _ in 0..<self.viewmodel.posts.count{
self.viewmodel.detailShowArray.append(false)
}
}
}
}
}
struct Panswipeview: View {
@Observedobject var viewmodel:viewmodel
@State var posts:[Post]
@State var screen = UIScreen.main.bounds.width - 30
var body: some View {
HStack(spacing:15){
Spacer()
ForEach(self.posts.indices,id: \.self){ index in
PanDetailCard(
viewmodel: viewmodel,index: index,imageURL: posts[index].image_url ?? "",word:posts[index].word
)
.offset(x:self.viewmodel.x)
.highPriorityGesture(DragGesture(minimumdistance: 20)
.onChanged({(value) in
if value.translation.width > 0{
self.viewmodel.x = value.location.x
}else {
self.viewmodel.x = value.location.x - self.screen
}
})
.onEnded({(value) in
if value.translation.width > 0{
if value.translation.width > ((self.viewmodel.swipePostsCount - 80) / 2) && Int(viewmodel.swipePostsCount) != self.getMid(){
self.viewmodel.swipePostsCount += 1
self.updateHeight(value: Int(viewmodel.swipePostsCount))
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
}
}else{
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
} }
}else{
if ((posts.count % 2) == 0){
if -value.translation.width > ((self.viewmodel.swipePostsCount - 80) / 2) && -Int(viewmodel.swipePostsCount) != self.getMid() - 1{
self.viewmodel.swipePostsCount -= 1
self.updateHeight(value: Int(viewmodel.swipePostsCount))
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
}
}else{
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
}
}
}else{
if -value.translation.width > ((self.viewmodel.swipePostsCount - 80) / 2) && -Int(viewmodel.swipePostsCount) != self.getMid(){
self.viewmodel.swipePostsCount -= 1
self.updateHeight(value: Int(viewmodel.swipePostsCount))
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
}
}else{
if ((posts.count % 2) == 0){
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount - 0.5)
}else{
self.viewmodel.x = (self.screen + 15 ) * (self.viewmodel.swipePostsCount)
}
}
}
}
})
)
}
Spacer()
}
.frame(width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height)
.background(Color.black.opacity(0.2))
.animation(.spring())
.navigationBarTitle("Detail",displayMode: .inline)
}
func getMid() -> Int{
return viewmodel.posts.count / 2
}
func updateHeight(value:Int){
var id : Int
if value < 0 {
id = -value + getMid()
}else{
id = getMid() - value
}
for i in 0..<viewmodel.posts.count{
self.viewmodel.detailShowArray[i] = false
}
self.viewmodel.detailShowArray[id] = true
}
}
struct PanDetailCard: View {
@Observedobject var viewmodel:viewmodel
@State var screen = UIScreen.main.bounds.width - 30
@State var url:String = ""
var index:Int
var imageURL:String
var word:String
var body: some View {
ScrollView{
Group{
HStack{
Text("Test")
Spacer()
}
HStack{
Spacer()
Text(Date(),style: .date)
.font(.footnote)
.foregroundColor(.secondary)
}
HStack{
Button(action: {
},label: {
HStack{
Image(systemName: "person.circle")
.foregroundColor(Color.black)
Text("user_name")
.foregroundColor(Color.black)
}
})
Spacer()
}
}
Group{
KFImage(URL(string: URL))
.cancelOndisappear(true)
.resizable()
.scaledToFit()
.frame(width: ScreenSize.screenWidth * 0.8,height: ScreenSize.screenWidth * 0.8)
Text("test")
.font(.title2)
.fontWeight(.bold)
vstack(alignment: .leading){
Text("test")
.fontWeight(.bold)
.foregroundColor(Color.black)
Text(word)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.blue)
Text("test")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(Color.black)
}
Divider()
vstack(alignment: .leading){
Text("sample,sample,sample")
.foregroundColor(Color.black)
}
}
}
.frame(width: UIScreen.main.bounds.width - 30,height: viewmodel.detailShowArray[index] ? (UIScreen.main.bounds.height * 0.7) + 60 : (UIScreen.main.bounds.height * 0.7) )
.background(Color.white)
.cornerRadius(10)
.onAppear(){
if imageURL != "" {
let storage = Storage.storage().reference()
storage.child(imageURL).downloadURL(completion: {(url,err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
self.url = "\(url!)"
})
}
}
}
}
Model.swift
import Foundation
import SwiftUI
import Firebase
class viewmodel: ObservableObject {
func getAllPosts(completion:@escaping(Bool) -> ()){
ref.collection("Posts").getDocuments{
(snap,err) in
guard let docs = snap else{
completion(false)
return
}
self.posts = []
docs.documentChanges.forEach{ (doc) in
let post = try! doc.document.data(as:Post.self)
self.posts.append(post!)
}
completion(true)
}
}
}
Xcode:12.3 版
iOS:14.0
翠鸟:6.2.1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。