当我们像iPhone上的相册应用一样滚动collectionView B时,如何滚动更改collectionView A?

时间:2019-05-26 02:57:29

标签: swift uicollectionview uiscrollviewdelegate

我想制作一个类似iPhone上的专辑应用程序的应用程序:当我滚动thumbnailCollectionView时,fullSizeCollectionView将相应地更改。 我在UIScrollViewDidScroll中做了一个fund didSettingfullSizeFollowThumbnail()函数,但是之后我的fullSizeCollectionView无法滚动。 除此之外,当我快速滚动缩略图时,我的fullSize无法正确更新。.请帮助我。

我认为原因是,当我拖动fullSizeCLV时,它是何时移至UIScrollViewDidScroll并运行func didSettingfullSizeFollowThumbnail(),以便无法滚动fullSize CLV。 func“ getThumbnailCenterIndexPath()”返回缩略图CLV中心项的indexPath

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if scrollView == fullSizeCLV {
        let currentPage = Int(targetContentOffset.pointee.x / UIScreen.main.bounds.width )
        let indexPath = IndexPath(item: currentPage, section: 0)
        thumbnailImageCLV.scrollToItem(at: indexPath, at: .left, animated: true)
    }
    if scrollView == thumbnailImageCLV {
        didForceThumbnailCenter()
    }
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if scrollView == thumbnailImageCLV {
        didForceThumbnailCenter()
    }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    didSettingFullsizeFollowThumbnail()
}

func didForceThumbnailCenter() {
    guard let indexPath = getThumbnailCenterIndexPath() else { return }
    if indexPath.item < pictures.count {
        thumbnailImageCLV.scrollToItem(at: indexPath, at: .left, animated: true)
    } else {
        let indexpath = IndexPath(item: pictures.count - 1, section: 0)
        thumbnailImageCLV.scrollToItem(at: indexpath, at: .left, animated: true)
    }
}

func didSettingFullsizeFollowThumbnail() {
    guard let indexPath = getThumbnailCenterIndexPath() else { return }
    if indexPath.item < pictures.count {
        fullSizeCLV.contentOffset.x = UIScreen.main.bounds.width * CGFloat(indexPath.item)
    } else {
        let indexpath = IndexPath(item: pictures.count - 1, section: 0)
        fullSizeCLV.contentOffset.x = UIScreen.main.bounds.width * CGFloat(indexpath.item)
    }
}[![enter image description here][1]][1]

0 个答案:

没有答案