2016-11-20 10 views
0

私はコレクションビューでページングを有効にして、各スワイプの最初の問題を別のセルではなくページで停止しました。Swift CollectionView垂直ページング

その後、コードを入力して、ScrollViewWillEndDeceleratingに手動でページングを処理しようとしました。 しかし、私の問題は、LayoutSubiewsで呼び出されない限り、collectionviews ContentOffsetの変更やPointへのスクロールが両方とも機能しないことです。コレクションビューに表示される唯一の場所です

コレクションビューの各セルはフルスクリーンです。私はレイアウトのサブビューでセルのサイズを扱います。

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    if let layout = customCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { 
     let itemWidth = view.frame.width 
     let itemHeight = view.frame.height 
     layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 
     layout.invalidateLayout() 
    } 


    let ip = IndexPath(row: 2, section: 0) 

    view.layoutIfNeeded() 
    customCollectionView.scrollToItem(at: ip, at: UICollectionViewScrollPosition.centeredVertically, animated: true) 

    let point = CGPoint(x: 50, y: 600) 
    customCollectionView.setContentOffset(point, animated: true) 

} 


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

    let pageHeight = customCollectionView.bounds.size.height 
    let videoLength = CGFloat(videoArray.count) 

    let minSpace:CGFloat = 10 

    var cellToSwipe = (scrollView.contentOffset.y)/(pageHeight + minSpace) + 0.5 
    if cellToSwipe < 0 { 
     cellToSwipe = 0 
    } 
    else if (cellToSwipe >= videoLength){ 
     cellToSwipe = videoLength - 1 
    } 
    let p = Int(cellToSwipe) 
    let roundedIP = round(Double(Int(cellToSwipe))) 
    let ip = IndexPath(row: Int(roundedIP), section: 0) 

    let ip = IndexPath(row: 2, section: 0) 
    view.layoutIfNeeded() 
    customCollectionView.scrollToItem(at: ip, at: UICollectionViewScrollPosition.centeredVertically, animated: true) 


} 

答えて

2

まず第一に、私はあなたのビューコントローラクラスはUICollectionViewDelegateFlowLayoutに準拠し、サイズに次のデリゲートメソッドを使用することをお勧めしますあなたのあなたのviewDidLoadコールでその後UICollectionViewCells

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let size = CGSize(width: view.frame.width, height: view.frame.height) 
    return size 
} 

customCollectionView.isPagingEnabled = true