0

I am making an app where you basically answer questions and then swipe left to get the next question. I like the transition of scroll views and would like to emulate that. I was wondering whether it would be recommended to create a custom transition between view controllers and or just implement a scroll view.

A scroll view seems easier, but not sure if it is possible (or if it is actually easier) when considering the following caveats.

Some caveats: - Can only swipe one direction (can't go back to previous questions) - Need to be able to load up another question from a datasource after swiping (obviously) - There is no set number of questions (so they could swipe forever and keep getting new questions)

Thanks

connorvo
  • 761
  • 2
  • 7
  • 21

2 Answers2

1

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.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
0

UIPageViewController might be a good approach.

Always return nil for viewControllerBefore to prevent "swiping back."

Return nil for viewControllerAfter until the user has answered the current question to prevent "swiping ahead" early.

DonMag
  • 69,424
  • 5
  • 50
  • 86