iosswiftuicollectionviewuiscrollviewcontentoffset

Swift -how is a collectionView's scrollView.content.y determined?


I have a collectionView with 5 cells. After I drag the collectionView down using this scrollView method I can get the following:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    let currentOffset = scrollView.contentOffset.y // 92 ???
    print("currentOffset: \(currentOffset)")

    let contentHeight = scrollView.contentSize.height // height of all the cells added together -616.310
    print("contentHeight: \(contentHeight)")

    let frame = scrollView.frame.size.height // height of the collectionView itself from it's topAnchor to it's bottomAnchor - 725
    print("frame: \(frame)")

    let maximumOffset = contentHeight - frame // -108.689453125 // distance between contentHeight (bottom of all the cells) and the frame
    print("maximumOffset: \(maximumOffset)")
}

The contentHeight is the height of all the cells added together which is: -616.310

The frame is the height of the collectionView itself in between it's topAnchor and it's bottomAnchor which is: 725

The maximumOffset is the distance between contentHeight (bottom of all the cells) and the frame which is: -108.689453125

What I can't figure out is where does the currentOffset which is 92 comes from.

This isn't very clear:

contentOffset

The point at which the origin of the content view is offset from the origin of the scroll view.

After dragging the collectionView, how is the currentOffset determined?

In the picture below at the bottom is the debugger with the print statements from the scrollViewDidEndDragging method

enter image description here

Not sure if this makes any difference or not but the collectionView's topAnchor and bottomAnchor are pinned to the view's safeAreaLayoutGuide ..., constant: 0 or it's anchored directly to it's top and bottom


Solution

  • It's how far the scrollView's top left hand corner (0,0) is from the top of the collectionView's top left hand corner (0,0) after the the collectionView is first dragged and let go.

    So it was initially pulled up 92 points (0,-92) then let go.