2017-04-13 121 views
0

我正在使用swift3我的应用程序包含用于水平滚动的UIscrollview。它包含带分页的图像。下面是它的代码示例。在iPad上水平滚动时uiscrollview laggging

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上执行时同样的应用水平滚动滞后。请告知

+0

你在哪里设置scrollview的contentSize? – karthikeyan

回答

1

为此目的使用UICollectionView要好得多。

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

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 
    } 

} 
+0

这是一个很好的答案。集合视图重用单元格,并不像滚动视图那样将所有内容都保存在内存中。滚动视图不会一次渲染所有内容,但整个视图层次结构仍在内存中。 – Alistra

0

你必须关闭滚动型的滞后。

self.scrollView.isPagingEnabled = false