Moh*_*ani 2 xcode uicollectionview swift
我已成功在索引处滚动项目,例如,如果用户点击按钮,则转到最后一项
这是我用过的代码
self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: self.array.count - 1, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)
Run Code Online (Sandbox Code Playgroud)
但我的问题是如何知道用户是在底部还是在顶部UICollectionView?
我可以通过创建2个函数来做到这一点,如果用户点击TOP UIButton它会上升,反之亦然......
但我只想用一个单一的方式做到这一点UIButton.因此,如果用户按下按钮,代码应该知道用户是否在底部并且向上,反之亦然.我查看了每个函数,UICollectionView但似乎没有return用户在哪个项目.
我想到了一些在逻辑上可能正确的东西
var someBool: Bool!
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let tempIndex: Int = self.array.count / 2
if indexPath.row > tempIndex{
someBool = true // UP
} else {
someBool = false // Down
}
...
}
@IBAction func buttonPressed{
if someBool{
self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)
} else {
self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: self.array.count - 1, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着我们假设一个数组有9个元素/ 2 = 4(INT),所以如果用户选择第9项,那么9> 4就会向下移动.
我相信这个代码只有在用户选择一个项目时才能正常工作,但是我需要另一种方法而不使用indexPath.row因为,我们假设用户没有选择任何项目...所以这段代码不起作用....
如果你从iOS 8.0支持
optional func collectionView(_ collectionView: UICollectionView,
willDisplayCell cell: UICollectionViewCell,
forItemAtIndexPath indexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
这个新API已经完全使用,因此您的代码将完美地工作,只需将代码从此didSelectItemAtIndexPath 方法移动到此方法,因为它每次调用并检查您的位置indexpath.row.让我知道它是否有帮助.
| 归档时间: |
|
| 查看次数: |
8621 次 |
| 最近记录: |