Rho*_*ick 12 objective-c uiscrollview ios uicollectionview uicollectionviewcell
我有一个垂直UICollectionView的每个单元占用整个self.view.frame我可以轻松向上滑到页面到下一个单元格,但我想按下一个按钮做同样的事情.
我尝试过使用:
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)
和:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)
他们工作但他们暂时"白出"当前的Cell以呈现nextCell,而刷卡在转换过程中显示两个单元格.
理想情况下我想使用:
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)
但我不知道如何访问nextCell indexPath...我试过:
NSIndexPath *nextIndexPath = [_collectionView indexPathForItemAtPoint:CGPointMake(25, (_collectionView.frame.size.height-25)*(_currentIndexRowForCellOnScreen+1))];
Run Code Online (Sandbox Code Playgroud)
哪里_currentIndexRowForCellOnScreen是indexPath.row对的UICollectionView的屏幕上的首次亮相:
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但当我把它放入:
- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
Run Code Online (Sandbox Code Playgroud)
它在第一个Cell之后返回NULL,并且没有动画....
任何方向将不胜感激.感谢您的时间.
MDB*_*983 40
假设你的collectionView只包含1个部分,并且假设每个项目占据整个框架,那么你可以做这样的事情;
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我遇到的最好的方法,诀窍是滚动到包含下一个对象的框架.请参考代码
@IBAction func actionPreviousFriends(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
/* -------------- display next friends action ----------------*/
@IBAction func actionNextFriends(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
self.moveToFrame(contentOffset: contentOffset)
}
func moveToFrame(contentOffset : CGFloat) {
let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
self.collectionView.scrollRectToVisible(frame, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
这是Swift版本
Swift 2.x:
let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems()
let currentItem: NSIndexPath = visibleItems.objectAtIndex(0) as! NSIndexPath
let nextItem: NSIndexPath = NSIndexPath(forRow: currentItem.item + 1, inSection: 0)
self.collectionView.scrollToItemAtIndexPath(nextItem, atScrollPosition: .Top, animated: true)
Run Code Online (Sandbox Code Playgroud)
Swift 3.x
let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems as NSArray
let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
let nextItem: IndexPath = IndexPath(item: currentItem.item + 1, section: 0)
self.collectionView.scrollToItem(at: nextItem, at: .top, animated: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22560 次 |
| 最近记录: |