我以编程方式创建UIImageViews并在它们之间滚动作为分页
如何以编程方式自动排版每个图像?
// manual attempt
viewDidLoad() {
self.scroll.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height)
let scrollWidth: CGFloat = self.scroll.frame.width
let imgOne = UIImageView(frame: CGRect(x:65, y:120, width:180, height:300))
let imgTwo = UIImageView(frame: CGRect(x:scrollWidth + 65, y:120, width:180, height:300))
let imgThree = UIImageView(frame: CGRect(x:scrollWidth*2 + 65, y:120,width:180, height:300))
imgOne.image = UIImage(named: "one.png")
imgTwo.image = UIImage(named: "two.png")
imgThree.image = UIImage(named: "three.png")
imgOne.center = self.view.center // works
imgTwo.center = ??
imgThree.center = ??
self.scroll.addSubview(imgOne)
self.scroll.addSubview(imgTwo)
self.scroll.addSubview(imgThree)
}
答案 0 :(得分:0)
使用UICollectionView
。您可以为图像之间的分页设置pagingEnabled
,在水平方向的布局中设置UICollectionViewScrollDirectionHorizontal
。请参阅使用它的好例子here。