2017-10-19 1 views
4

enter image description hereWie zu bestimmen, wann eine benutzerdefinierte UICollectionViewCell ist zu 100% auf den Bildschirm

Aus dem Diagramm oben I UICollectionView mit 4 individuellen Zellen hat. Zu jeder Zeit können 2 oder 3 Zellen auf dem Bildschirm sein. Wie kann ich feststellen, wenn "Zelle 1" oder "Zelle 2" zu 100% auf dem Bildschirm angezeigt wird?

Beide

collectionView.visibleCells 
collectionView.indexPathsForVisibleItems 

Return-Arrays und Ihnen nicht sagen, ob welche Zelle 100% auf dem Bildschirm.

Im Falle des Bildes, wird die folgende Anzeige sein auf didSelectItemAt

collectionView.visibleCells

[<Shot_On_Goal.MainCollectionViewCell: 0x101f525c0; baseClass = UICollectionViewCell; frame = (190 7.66667; 454 350); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0237300>>, <Shot_On_Goal.HeaderCollectionViewCell: 0x101f4d580; baseClass = UICollectionViewCell; frame = (10 0; 170 365); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0236800>>, <Shot_On_Goal.TheirHockeyNetCollectionViewCell: 0x101f55520; baseClass = UICollectionViewCell; frame = (654 7.66667; 454 350); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1c0238fe0>>] 

collectionView.indexPathsForVisibleItems

[[0, 1], [0, 0], [0, 2]] 
+0

Haben Sie darüber nachgedacht, mit [CGRectContainsRect] (https://developer.apple.com/documentation/coregraphics/1454186-cgrectcontainsrect) ? – Wez

+0

Sieht aus, als ob jemand dies bereits mit einem [tableViewCell] (https://stackoverflow.com/questions/9831485/best-way-to-check-if-guidableviewcell-is-completely-visible) getan hat. – Wez

Antwort

4

Dies wird ein Array von IndexPaths für die vollständig sichtbaren Zellen zurück:

func fullyVisibleCells(_ inCollectionView: UICollectionView) -> [IndexPath] { 

    var returnCells = [IndexPath]() 

    var vCells = inCollectionView.visibleCells 
    vCells = vCells.filter({ cell -> Bool in 
     let cellRect = inCollectionView.convert(cell.frame, to: inCollectionView.superview) 
     return inCollectionView.frame.contains(cellRect) 
    }) 

    vCells.forEach({ 
     if let pth = inCollectionView.indexPath(for: $0) { 
      returnCells.append(pth) 
     } 
    }) 

    return returnCells 

} 

@IBAction func test(_ sender: Any) { 

    let visCells = fullyVisibleCells(self.collectionView) 
    print(visCells) 

} 
+0

Danke für die Lösung, funktioniert super. –

1

Sie können Ihre visibleCells Array auswählen, wenn der Rahmen zu prüfen, von Ihre Zelle befindet sich im Rahmen Ihrer SammlungView:

var visibleCells = self.collectionView?.visibleCells 
    visibleCells = visibleCells?.filter({ cell -> Bool in 
     return self.collectionView?.frame.contains(cell.frame) ?? false 
    }) 
    print (visibleCells) 
Verwandte Themen