我正在使用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上执行相同的应用程序水平滚动是滞后的。请指教
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
你必须关闭scrollview的滞后。
self.scrollView.isPagingEnabled = false