After reading your requirement, the most suitable UI widget would be UICollectionView
.
Here are some points which would help in cover up your requirement.
1. Sizing the cell
You might want to show the question and answer related views across the whole view, in that case your collection view will be pinned to top
, bottom
, left
, right
with constraint and your cell size would be equal to the collection view bound's size.
Implement the UICollectionViewFlowLayoutDelegate
's delegate
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.bounds.size
}
2. Enable Paging
Enable the paging of the collectionView, so that the scroll does not stays at any breaking (i.e scrolling stopped at between two questionary cells).
collectionView.isPagingEnabled = true
3. Locking the scroll direction (only scrolling to right)
If you target for iOS 11.0+
you may have a look @ UICollectionViewDragDelegate
(appledoc) which will provide rich set of functionality to achieve this.
Otherwise i like to suggest you to use following
public func scrollViewDidScroll(_ scrollView: UIScrollView)
which will notify you every single scroll user doing in your CollectionView
. It's a method from UIScrollView
and UICollectionView
inherit it from UIScrollView
.
var lastOffsetX = -1
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
// for the first time only
if lastOffset < 0 {
lastOffsetX = scrollView.contentOffset.x
return
}
if lastOffset > scrollView.contentOffset.x {
// stick to the right most position -- not enabling the user to go left
scrollView.contentOffset.x = lastOffset
} else {
// take the user ahead to the right position
lastOffset = scrollView.contentOffset.x
}
}
While dealing this way you may face a issue that setting contentOffset inside the scrollViewDidScroll
might end up firing up the scrollViewDidScroll
method. If you face this you may take a look at this answer. Surely i didn't ran this code, but with few exemption, it should work.
Hope it helps, Happy coding.