我有一个带有图像的 Collection View 。我想在按下按钮时执行滚动。我在谷歌上搜索过我发现只能设置 contentoffset 但我想滚动动画(如果内容超过滚动区域,滚动应该弹跳)。我添加了一个 image为了向您展示我必须做什么,图片中有一个 Collection View ,两侧的小箭头是按钮。
@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 的 scrollToItemAtIndexPath(_:atScrollPosition:animated:)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/scrollToItemAtIndexPath:atScrollPosition:animated :
例如:
let indexPath = NSIndexPath(forRow: 3, inSection: 0)
imageCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: true)
关于ios - 如何在按下按钮时在 Collection View 中滚动(水平)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024031/