【问题标题】:uiscrollview laggging while scrolling horizontally on iPaduiscrollview 在 iPad 上水平滚动时滞后
【发布时间】:2017-09-09 11:27:27
【问题描述】:

我正在使用 swift3,我的应用包含用于水平滚动的 UIscrollview。它包含带有分页的图像。下面是它的代码示例。

for index in 0..<storyImages.count { 
       frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
       frame.size = self.scrollView.frame.size
       self.scrollView.isPagingEnabled = true
        print(self.scrollView.bounds.width * CGFloat(index))
      let rect = CGRect(x: CGFloat(self.scrollView.bounds.width * CGFloat(index)), y: 0, width: scrollViewWidth , height: scrollViewHeight)
      let imageView = UIImageView()
        imageView.frame = rect
      imageView.image = storyImages[index]
     self.scrollView.addSubview(imageView)           
  }

它在所有 iPhone 上都能完美运行,但是在 iPad 水平滚动上执行相同的应用程序时会出现延迟。请指教

【问题讨论】:

  • 你在哪里设置scrollview的contentSize?

标签: ios swift ipad uiscrollview


【解决方案1】:

最好使用 UICollectionView 来达到这个目的。

将滚动方向设置为水平并为集合视图启用分页。 和最小间距

extension FirstViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = indexPath.row % 2 == 0 ? #colorLiteral(red: 1, green: 0.3126001954, blue: 1, alpha: 1) : #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)
        return cell
    }

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

}

extension FirstViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.frame.size
    }

}

【讨论】:

  • 这是一个很好的答案。集合视图重用单元格,并且不会像滚动视图那样将所有内容都保存在内存中。滚动视图不会一次渲染所有内容,但整个视图层次结构仍然在内存中。
【解决方案2】:

你必须关闭滚动视图的滞后。

self.scrollView.isPagingEnabled = false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-29
    • 2018-07-13
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多