gpt4 book ai didi

ios - 在 UIScrollView 中高效加载多个 View

转载 作者:行者123 更新时间:2023-11-29 05:39:36 25 4
gpt4 key购买 nike

我有一个 UIScrollView,我试图将其用作图像查看器。为此,我启用了分页,并将“幻灯片”添加到每个图像的 View 中,包括 UIImageView 和多个标签和按钮。当我只有几张幻灯片要显示时,这非常有效,但我需要 100 多张幻灯片,而且我遇到了非常糟糕的性能问题。当我呈现 ViewController 并因此设置 ScrollView 时,我得到了 10-15 秒的延迟。显然加载这么多 View 有点太多了。

所以我想知道你们中是否有人知道如何提高效率。

我尝试在之前的 VC 中制作幻灯片数组,然后传递它,而不是当场创建它,这有一点帮助,但不足以让它感觉可以接受,特别是因为更改设备方向将需要我再次设置 ScrollView(因为幻灯片高度/宽度将关闭)。

以下是设置幻灯片并将其呈现在 ScrollView 上的函数:

    func createSlides() -> [Slide] {
print("creating Slides")

let Essence = EssenceModel.Essence
var ImageArray = [Slide]()

var slide: Slide
var count = 0


for img in Essence{
count += 1

slide = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slide
slide.imageView.image = UIImage(named: img.imageUrl)
slide.isUserInteractionEnabled = true
slide.textLabel.text = img.description
slide.likeButton.imageView?.contentMode = .scaleAspectFit
slide.hero.id = img.heroID

slide.tag = count

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(showOrHide))
slide.imageView.addGestureRecognizer(tapGesture)
let dismissGesture = UITapGestureRecognizer(target: self, action: #selector(dismissVC))

slide.backButton.addGestureRecognizer(dismissGesture)
slide.backButton.isUserInteractionEnabled = true
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipedUp))
swipeUp.direction = .up
slide.addGestureRecognizer(swipeUp)

let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(swipedDown))
swipeDown.direction = .down
slide.addGestureRecognizer(swipeDown)

let slideRecognizer = UITapGestureRecognizer(target: self, action: #selector(startSlideshow))
slide.slideButton.addGestureRecognizer(slideRecognizer)
slide.likeButton.imageView?.contentMode = .scaleAspectFit

slide.setupZoom()

ImageArray.append(slide)
}
count = 0
print(ImageArray.count)
return ImageArray
}


func setupSlideScrollView(slides : [Slide]) {
scrollView.subviews.forEach { $0.removeFromSuperview() }

scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
scrollView.isPagingEnabled = true

for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)

scrollView.addSubview(slides[i])

}

}

正如我所说,我正在寻找以任何方式提高效率的方法,以便我可以实际使用它。我最好只加载我所在的幻灯片,下一张和上一张,但我不知道如何去做。

这里还有一个屏幕截图,以便您可以看到它的样子。

w w

最佳答案

这样做会更好:

1)添加UICollectionView

2)添加具有restoreID的UITableViewCell

3)向 Controller / View 添加关系

4) 设置水平滚动方向:https://www.screencast.com/t/wmPiwVdY

5)然后创建类似的逻辑:

class ImageCollectionViewVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!
private var images = [Slide]()

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let imageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageSlideCellRestorationID", for: indexPath) as? ImageSlideCell else {
return ImageSlideCell()
}
imageCell.image.image = UIImage(named: images[indexPath.row].imageUrl)
return imageCell
}
}


class ImageSlideCell: UICollectionViewCell {
@IBOutlet weak var image: UIImageView!
}

关于ios - 在 UIScrollView 中高效加载多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690035/

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