gpt4 book ai didi

ios - 如何在按下按钮时在 Collection View 中滚动(水平)?

转载 作者:行者123 更新时间:2023-11-28 08:51:42 25 4
gpt4 key购买 nike

我有一个带有图像的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com