gpt4 book ai didi

ios - 在 ScrollView 中隐藏未使用的图像(Swift)

转载 作者:行者123 更新时间:2023-11-28 06:14:13 25 4
gpt4 key购买 nike

我需要隐藏未使用的图像。怎么做?在我的代码中,我有三个图像,但并不总是使用所有图像。如何隐藏第三张图片?稍后,如果我添加第三张图片,我需要在不更改代码的情况下再次取消隐藏该图片。

import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage

class ScrollViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!

var image = [String]()

override func viewDidLoad() {
super.viewDidLoad()

loadImages()

}
func loadImages() {

Database.database().reference().child("Студии2").observeSingleEvent(of: .value, with: { (snapshot) in

let newImages = [String]()

for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "Photo")
let dict = imageSnap.value as! [String: AnyObject]
let url = dict["image"] as? String ?? ""
let url2 = dict["image1"] as? String ?? ""
let url3 = dict["image3"] as? String ?? ""
self.image.append(url)
self.image.append(url2)
self.image.append(url3)

for i in 0..<self.image.count {
let imagesArray = UIImageView()
imagesArray.sd_setImage(with: URL(string: self.image[i]))
let xPos = self.view.frame.width * CGFloat(i)
imagesArray.frame = CGRect(x: xPos, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)


self.scrollView.contentSize.width = self.scrollView.frame.width * CGFloat(i + 1)
self.scrollView.addSubview(imagesArray)
}
}
self.image = newImages
})
}

感谢您的帮助。

最佳答案

您可以过滤掉空对象,以便内容大小正确

这是怎么做的

func loadImages() {
Database.database().reference().child("Студии2").observeSingleEvent(of: .value, with: { snapshot in

for child in snapshot.children {
guard let snap = child as? DataSnapshot else { return }
let imageSnap = snap.childSnapshot(forPath: "Photo")
guard let dict = imageSnap.value as? [String: AnyObject] else { return }

self.image.append(dict["image"] as? String ?? "")
self.image.append(dict["image1"] as? String ?? "")
self.image.append(dict["image3"] as? String ?? "")

let links = self.image.filter { !$0.isEmpty }

let size = self.view.frame.size

links.enumerated().forEach {
let imageView = UIImageView()
imageView.sd_setImage(with: URL(string: $0.element))
let x = size.width * CGFloat($0.offset)
imageView.frame = CGRect(x: x, y: 0, width: size.width, height: size.height)

self.scrollView.addSubview(imageView)
}

self.scrollView.contentSize.width = size.width * CGFloat(links.count)
}
self.image = [String]()
})
}

关于ios - 在 ScrollView 中隐藏未使用的图像(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45606053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com