0

My collection view has several sections and the section I am having trouble with is the "메인배너" (aka mainBanner) section.

The layout of the 메인배너 section is as follows and you can think of a carousel view.

let itemSize = NSCollectionLayoutSize(
    widthDimension: .fractionalWidth(1.0),
    heightDimension: .fractionalHeight(1.0)
)

let item = NSCollectionLayoutItem(
    layoutSize: itemSize
)

let groupSize = NSCollectionLayoutSize(
    widthDimension: .fractionalWidth(1.0),
    heightDimension: .fractionalHeight(0.6)
)

let group = NSCollectionLayoutGroup.horizontal(
    layoutSize: groupSize,
    subitems: [item]
)

let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .groupPagingCentered

I called the bannerTimer() method in viewDidLoad() to scroll the 메인배너 section every 3 seconds.

// Presenter

func bannerTimer() {
    Timer.scheduledTimer(
        withTimeInterval: 3,
        repeats: true
    ) { [weak self] _ in
        guard let page = self?.currentBannerPage,
              let mainBannerCount = self?.model.goodsList[.메인배너]?.count else {return}

        let section = HomeCollectionViewSectionKind.메인배너.rawValue
        let indexPath = IndexPath(item: page, section: section)

        if page == mainBannerCount - 1 {
            self?.currentBannerPage = 0
             self?.viewController?.bannerMove(
                 indexPath,
                 position: .right
             )
        }

        self?.currentBannerPage += 1
        self?.viewController?.bannerMove(
            indexPath,
            position: .right
        )
    }
}

// ViewController

func bannerMove(
    _ indexPath: IndexPath,
    position: UICollectionView.ScrollPosition
) {
    collectionView.scrollToItem(
        at: indexPath,
        at: position,
        animated: true
    )
}

But here an issue arises
When the bannerMove() method is called after 3 seconds while the collectionView is vertically scrolled, it moves to the indexPath location passed as a parameter
I know the reason is because I used scrollToItem()
But I don't want to be bothered with scrolling the collectionView vertically, while the 메인배너 section auto-scrolls itself
Is there any way? Or should I change to a combination of TableView and CollectionView?

서승우
  • 1
  • 2

0 Answers0