【发布时间】:2015-12-01 15:31:26
【问题描述】:
我有一个包含图像的集合视图。我想在按钮按下时执行滚动。我在谷歌上搜索过我发现只设置 contentoffset 但我想滚动动画(如果内容超出滚动区域,滚动应该反弹)。我添加了一个 image 来告诉你我到底要做什么,在图片有一个集合视图,两边的小箭头是按钮。
@IBAction func moveScrollLeft(sender: UIButton) {
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.imageCollectionView.contentOffset.x -= 50
}, completion: nil)
print(imageCollectionView.contentOffset.x)
}
@IBAction func moveScrollRight(sender: UIButton) {
UIView.animateWithDuration(0.2, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.imageCollectionView.contentOffset.x += 50
}, completion: nil)
print(imageCollectionView.contentOffset.x)
}
【问题讨论】:
-
UICollectionView 是 UIScrollView 的子类。您可以使用 collectionView.setContentOffset(offset, animated:true)。关于反弹,您必须检查 contentOffset.x + bounds.width > contentSize.width 并为反弹设置动画。
标签: ios swift uicollectionview