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

标签 ios swift uicollectionview

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

相关文章:

iphone - 如何以字符串形式记录 nsdata

ios - 重新加载表格 View 后出现抖动

swift - FireBase - 将子项(按原样)移动到不同的路径

ios - CollectionView 未按预期垂直显示

ios - managedObject 类和我自己的模型类之间的关系?

ios - 在一天中的特定时间从 iOS 将设备位置发送到 Web 服务

ios - 如何将标题/按钮添加到 UIViewController 内添加的 UINavigationController?

ios - Swift 中的可选闭包属性

ios - CollectionUIView重新加载数据在获取数据后不更新显示

ios - 根据 UIsegmented 控件名称执行操作