2016-06-05 12 views
1

[! [Kollektion Beispiel] [1]] [1]UICollectionView individuell horizontale Paging Layout

Ich versuche, dieses Verhalten Ansicht mit einer Sammlung zu emulieren. Ich habe damit angefangen, mit dieser Methode zu arbeiten und es hat mich näher gebracht. Obwohl ich immer weiter nach rechts auf meinem horizontalen Paging CollectionView wische, verschiebt sich der Inhalt immer weiter nach links. Auch der Abstand zwischen den Zellen ist ausgeschaltet. Ich glaube, es erfordert ein benutzerdefiniertes Layout, bin mir aber nicht sicher.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    return CGSize(width: collectionView.frame.width - 20, height: collectionView.frame.height - 20) 

} 
+0

, was die eigentliche Frage ist hier Zelle in der Bildschirmmitte? – Shubhank

Antwort

6

es hängt von 3 Faktoren 1) Abschnitt Insets 2) Zellenabstand 3) Zellengröße

Jede Änderung in jedem müssen Sie andere für Ihren Fall ändern

1) Set links & rechts mit 20

enter image description here

2) eingestellt Zellenabstand auf 10

func collectionView(collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 10 
} 
func collectionView(collectionView: UICollectionView, layout 
    collectionViewLayout: UICollectionViewLayout, 
    minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 10 
} 

3) Set Zellengröße

func collectionView(collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     return CGSize(width: collectionView.frame.width/1.15 ,height: collectionView.frame.height) 
} 

4) dies wird

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 

    let pageWidth:CGFloat = scrollView.frame.width/1.15 + cellSpacing ; 

    let currentOffset:CGFloat = scrollView.contentOffset.x; 
    let targetOffset:CGFloat = targetContentOffset.memory.x 
    var newTargetOffset:CGFloat = 0; 

    if targetOffset > currentOffset 
    { 
     newTargetOffset = ceil(currentOffset/pageWidth) * pageWidth; 
    } 
    else 
    { 
     newTargetOffset = floor(currentOffset/pageWidth) * pageWidth; 
    } 

    if newTargetOffset < 0 
    { 
     newTargetOffset = 0 
    } 
    else if newTargetOffset > scrollView.contentSize.width 
    { 
    newTargetOffset = scrollView.contentSize.width; 
    } 

    targetContentOffset.memory.x = currentOffset 

    scrollView.setContentOffset(CGPointMake(newTargetOffset, 0), animated: true) 

} 
+0

Siehe diesen Link – Preetha

+0

http://stackoverflow.com/questions/19301762/uicollectionview-horizontal-scroll-horizontal-layout – Preetha

+0

Wie sind Sie mit 1.15 gekommen? – mparrish91

Verwandte Themen