2017-04-26 67 views
1

我有一篇文章的集合視圖。我希望能夠跟蹤當前用戶查看的帖子。我怎樣才能避免某些問題,如快速滾動,這將不允許我使用結束減速。如何查看哪個UICollectionView單元格已滾動

這個想法是知道已經通過屏幕的用戶可見區域的所有單元格。我的收藏單元格是全屏。 任何想法?

+0

如果用戶快速滾動通過您的帖子也許它確定不認爲他們被查看? – Danoram

+0

我覺得'UIScrollViewDelegate'會是最好的。不要使用任何會損害性能的定時器。不知道爲什麼你認爲結束減速不合適。 – user1046037

+0

並使用indexPathForVisibleItems – user1046037

回答

0

我認爲你可以使用willDisplay跟蹤開始時間顯示單元

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 

    // Save start time view at indexPath. 
} 

並使用didEndDisplaying跟蹤結束時間顯示單元

func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 

    // End time at indexPath 
    // End - Start time > "time of view" => Viewed 
} 
0

這爲我工作在一個類似的功能,currentlyVisibleIndex持有已查看的collectionView單元格的索引值。對於您的實現可以保持其已被視爲

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){ 
     /* set pagewidth to collectionview cell width*/ 
     let pageWidth = Float(collectionView.bounds.width) 
     let currentOffset : Float = Float(scrollView.contentOffset.x) 
     let targetOffset = Float(targetContentOffset.memory.x) 
     var newTargetOffset : Float = 0.0 

    if targetOffset > currentOffset { 
     newTargetOffset = ceilf(currentOffset/(pageWidth)) * pageWidth 
     /* if collectionview swipped right increment the curentlyVisibleIndex*/ 
     if currentlyVisibleIndex != (arrayOfModelForCollectionView.count - 1) { 
      currentlyVisibleIndex += 1 
     } 
    } 
    else{ 
     newTargetOffset = floorf(currentOffset/(pageWidth)) * pageWidth 
     /*if collectionview swipped left decrement the currentlyVisibleCounter */ 
     if currentlyVisibleIndex != 0 { 
      currentlyVisibleIndex -= 1 
     } 
    } 
    if newTargetOffset < 0{ 
     newTargetOffset = 0 
    }else if newTargetOffset > Float(scrollView.contentSize.width){ 
     newTargetOffset = Float(scrollView.contentSize.width) 
    } 
    targetContentOffset.memory.x = CGFloat(currentOffset) 
    scrollView.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: 0), animated: true) 
} 
0

由於您UICollectionViewCell的細胞的指數的一個數組是全屏幕,你可以做的就是實現UIScrollView委託方法,scrollViewDidScroll和計算單元目前可見。類似這樣的:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 

    let index = scrollView.contentOffset.y/view.bounds.size.height 
} 

這將顯示當前顯示給用戶的任何索引。由於scrollViewDidScroll將始終被調用,因此可以使用此方法確定用戶當前所在的索引。

相關問題